Skip to content

Commit

Permalink
Accept edits
Browse files Browse the repository at this point in the history
http://guides.emberjs.com/v1.10.0/getting-started/accepting-edits/

```
ember g component edit-todo
rm app/templates/components/edit-todo.hbs
sed -i .bak 's/Component/TextField/g' app/components/edit-todo.js && rm app/components/edit-todo.js.bak
```
  • Loading branch information
chrislopresto committed Mar 12, 2015
1 parent ae3f025 commit 00ce0d9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/components/edit-todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Ember from 'ember';

export default Ember.TextField.extend({
didInsertElement: function() {
this.$().focus();
}
});
14 changes: 14 additions & 0 deletions app/controllers/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ export default Ember.ObjectController.extend({
actions: {
editTodo: function() {
this.set('isEditing', true);
},
acceptChanges: function() {
this.set('isEditing', false);

if (Ember.isEmpty(this.get('model.title'))) {
this.send('removeTodo');
} else {
this.get('model').save();
}
},
removeTodo: function () {
var todo = this.get('model');
todo.deleteRecord();
todo.save();
}
},

Expand Down
3 changes: 2 additions & 1 deletion app/templates/todos.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
{{#each todo in model itemController="todo"}}
<li {{bind-attr class="todo.isCompleted:completed todo.isEditing:editing"}}>
{{#if todo.isEditing}}
<input class="edit">
{{edit-todo class="edit" value=todo.title focus-out="acceptChanges"
insert-newline="acceptChanges"}}
{{else}}
{{input type="checkbox" checked=todo.isCompleted class="toggle"}}
<label {{action "editTodo" on="doubleClick"}}>{{todo.title}}</label><button class="destroy"></button>
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/components/edit-todo-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
moduleForComponent,
test
} from 'ember-qunit';

moduleForComponent('edit-todo', {
// specify the other units that are required for this test
// needs: ['component:foo', 'helper:bar']
});

test('it renders', function(assert) {
assert.expect(2);

// creates the component instance
var component = this.subject();
assert.equal(component._state, 'preRender');

// renders the component to the page
this.render();
assert.equal(component._state, 'inDOM');
});

0 comments on commit 00ce0d9

Please sign in to comment.