Toggling between Showing and Editing States Edit Page


TodoMVC allows users to double click each todo to display a text <input> element where the todo's title can be updated. Additionally the <li> element for each todo obtains the CSS class editing for style and positioning.

We'll update the application to allow users to toggle into this editing state for a todo. In index.html update the contents of the {{each}} Handlebars helper to:

1
2
3
4
5
6
7
8
9
10
11
12
 <!--- ... additional lines truncated for brevity ... -->
{{#each itemController="todo"}}
  <li {{bind-attr class="isCompleted:completed isEditing:editing"}}>
    {{#if isEditing}}
      <input class='edit'>
    {{else}}
      {{input type="checkbox" checked=isCompleted class="toggle"}}
      <label {{action "editTodo" on="doubleClick"}}>{{title}}</label><button class="destroy"></button>
    {{/if}}
  </li>
{{/each}}
 <!--- ... additional lines truncated for brevity ... -->

The above code applies three new behaviors to our application: it applies the CSS class editing when the controller's isEditing property is true and removes it when the isEditing property is false. We add a new {{action}} helper to the <label> so double-clicks will call editTodo on this todo's controller. Finally, we wrap our todo in a Handlebars {{if}} helper so a text <input> will display when we are editing and the todos title will display when we are not editing.

Inside js/controllers/todo_controller.js we'll implement the matching logic for this template behavior:

1
2
3
4
5
6
7
8
9
10
// ... additional lines truncated for brevity ...
actions: {
   editTodo: function () {
     this.set('isEditing', true);
   }
 },

isEditing: false,

// ... additional lines truncated for brevity ...

Above we defined an initial isEditing value of false for controllers of this type and said that when the editTodo action is called it should set the isEditing property of this controller to true. This will automatically trigger the sections of template that use isEditing to update their rendered content.

Reload your web browser to ensure that no errors occur. You can now double-click a todo to edit it.

Live Preview

Ember.js • TodoMVC

Additional Resources