Commit ef7de978 authored by Cory Forsyth's avatar Cory Forsyth

Change code to be more idiomatic ember

 * Focus new-todo input on load
 * Use CP's for reamining and completed
 * Add pluralize helper, remove html from controller
 * Add TodoController.removeTodo method
 * We want to avoid debouncing a call to `send`, so we make `removeTodo`
   a method on the controller, and have the `removeTodo` *action* simply
   call that method.
 * Move Todo auto-save from model into controller
parent 9bd47101
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
{{input type="checkbox" id="toggle-all" checked=allAreDone}} {{input type="checkbox" id="toggle-all" checked=allAreDone}}
</section> </section>
<footer id="footer"> <footer id="footer">
<span id="todo-count">{{{remainingFormatted}}}</span> <span id="todo-count"><strong>{{remaining.length}}</strong> {{pluralize 'item' remaining.length}} left</span>
<ul id="filters"> <ul id="filters">
<li> <li>
{{#link-to "todos.index" activeClass="selected"}}All{{/link-to}} {{#link-to "todos.index" activeClass="selected"}}All{{/link-to}}
...@@ -42,9 +42,9 @@ ...@@ -42,9 +42,9 @@
{{#link-to "todos.completed" activeClass="selected"}}Completed{{/link-to}} {{#link-to "todos.completed" activeClass="selected"}}Completed{{/link-to}}
</li> </li>
</ul> </ul>
{{#if hasCompleted}} {{#if completed.length}}
<button id="clear-completed" {{action "clearCompleted"}}> <button id="clear-completed" {{action "clearCompleted"}}>
Clear completed ({{completed}}) Clear completed ({{completed.length}})
</button> </button>
{{/if}} {{/if}}
</footer> </footer>
...@@ -72,5 +72,7 @@ ...@@ -72,5 +72,7 @@
<script src="js/controllers/todos_controller.js"></script> <script src="js/controllers/todos_controller.js"></script>
<script src="js/controllers/todo_controller.js"></script> <script src="js/controllers/todo_controller.js"></script>
<script src="js/views/edit_todo_view.js"></script> <script src="js/views/edit_todo_view.js"></script>
<script src="js/views/todos_view.js"></script>
<script src="js/helpers/pluralize.js"></script>
</body> </body>
</html> </html>
...@@ -22,15 +22,15 @@ Todos.TodoController = Ember.ObjectController.extend({ ...@@ -22,15 +22,15 @@ Todos.TodoController = Ember.ObjectController.extend({
// enter (once via 'insert-newline' and once via 'focus-out'). // enter (once via 'insert-newline' and once via 'focus-out').
// //
// We debounce our call to 'removeTodo' so that it only gets // We debounce our call to 'removeTodo' so that it only gets
// sent once. // made once.
Ember.run.debounce(this, this.send, 'removeTodo', 0); Ember.run.debounce(this, 'removeTodo', 0);
} else { } else {
var todo = this.get('model'); var todo = this.get('model');
todo.set('title', bufferedTitle); todo.set('title', bufferedTitle);
todo.save(); todo.save();
} }
// Re-set our newly edited title to persist it's trimmed version // Re-set our newly edited title to persist its trimmed version
this.set('bufferedTitle', bufferedTitle); this.set('bufferedTitle', bufferedTitle);
this.set('isEditing', false); this.set('isEditing', false);
}, },
...@@ -41,10 +41,18 @@ Todos.TodoController = Ember.ObjectController.extend({ ...@@ -41,10 +41,18 @@ Todos.TodoController = Ember.ObjectController.extend({
}, },
removeTodo: function () { removeTodo: function () {
var todo = this.get('model'); this.removeTodo();
todo.deleteRecord();
todo.save();
} }
} },
removeTodo: function () {
var todo = this.get('model');
todo.deleteRecord();
todo.save();
},
saveWhenCompleted: function () {
this.get('model').save();
}.observes('isCompleted')
}); });
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
Todos.TodosController = Ember.ArrayController.extend({ Todos.TodosController = Ember.ArrayController.extend({
actions: { actions: {
createTodo: function () { createTodo: function () {
var title, todo; var title, todo;
...@@ -25,37 +24,26 @@ Todos.TodosController = Ember.ArrayController.extend({ ...@@ -25,37 +24,26 @@ Todos.TodosController = Ember.ArrayController.extend({
}, },
clearCompleted: function () { clearCompleted: function () {
var completed = this.filterProperty('isCompleted', true); var completed = this.get('completed');
completed.invoke('deleteRecord'); completed.invoke('deleteRecord');
completed.invoke('save'); completed.invoke('save');
}, },
}, },
remaining: function () { /* properties */
return this.filterProperty('isCompleted', false).get('length');
}.property('@each.isCompleted'),
remainingFormatted: function () {
var remaining = this.get('remaining');
var plural = remaining === 1 ? 'item' : 'items';
return '<strong>%@</strong> %@ left'.fmt(remaining, plural);
}.property('remaining'),
completed: function () { remaining: Ember.computed.filterBy('content', 'isCompleted', false),
return this.filterProperty('isCompleted', true).get('length'); completed: Ember.computed.filterBy('content', 'isCompleted', true),
}.property('@each.isCompleted'),
hasCompleted: function () {
return this.get('completed') > 0;
}.property('completed'),
allAreDone: function (key, value) { allAreDone: function (key, value) {
if (value !== undefined) { if (value !== undefined) {
this.setEach('isCompleted', value); this.setEach('isCompleted', value);
return value; return value;
} else { } else {
return !!this.get('length') && var length = this.get('length');
this.everyProperty('isCompleted', true); var completedLength = this.get('completed.length');
return length > 0 && length === completedLength;
} }
}.property('@each.isCompleted') }.property('length', 'completed.length')
}); });
/*global Todos, Ember */
'use strict';
Ember.Handlebars.helper('pluralize', function (singular, count) {
/* From Ember-Data */
var inflector = new Ember.Inflector(Ember.Inflector.defaultRules);
return count === 1 ? singular : inflector.pluralize(singular);
});
...@@ -3,9 +3,5 @@ ...@@ -3,9 +3,5 @@
Todos.Todo = DS.Model.extend({ Todos.Todo = DS.Model.extend({
title: DS.attr('string'), title: DS.attr('string'),
isCompleted: DS.attr('boolean'), isCompleted: DS.attr('boolean')
saveWhenCompletedChanged: function () {
this.save();
}.observes('isCompleted')
}); });
/*global Todos, Ember */
'use strict';
Todos.TodosView = Ember.View.extend({
focusInput: function () {
this.$('#new-todo').focus();
}.on('didInsertElement')
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment