diff --git a/index.html b/index.html index 0938c0d..b742e33 100644 --- a/index.html +++ b/index.html @@ -6,6 +6,21 @@ + + + + diff --git a/js/app.js b/js/app.js index c9fdf29..f8b336e 100644 --- a/js/app.js +++ b/js/app.js @@ -1,3 +1,5 @@ window.Todos = Ember.Application.create(); -Todos.ApplicationAdapter = DS.FixtureAdapter.extend(); +Todos.ApplicationAdapter = DS.LSAdapter.extend({ + namespace: 'todos-emberjs' +}); diff --git a/js/controllers/todo_controller.js b/js/controllers/todo_controller.js index 47b50b3..2480a14 100644 --- a/js/controllers/todo_controller.js +++ b/js/controllers/todo_controller.js @@ -2,6 +2,22 @@ Todos.TodoController = Ember.ObjectController.extend({ actions: { editTodo: function () { this.set('isEditing', true); + }, + acceptChanges: function () { + if (this.get('isEditing')) { + 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(); } }, diff --git a/js/controllers/todos_controller.js b/js/controllers/todos_controller.js index aa1efdf..ceee8b9 100644 --- a/js/controllers/todos_controller.js +++ b/js/controllers/todos_controller.js @@ -16,6 +16,11 @@ Todos.TodosController = Ember.ArrayController.extend({ // Save the new model todo.save(); + }, + clearCompleted: function () { + var completed = this.filterProperty('isCompleted', true); + completed.invoke('deleteRecord'); + completed.invoke('save'); } }, @@ -26,5 +31,23 @@ Todos.TodosController = Ember.ArrayController.extend({ inflection: function () { var remaining = this.get('remaining'); return remaining === 1 ? 'item' : 'items'; - }.property('remaining') + }.property('remaining'), + + hasCompleted: function () { + return this.get('completed') > 0; + }.property('completed'), + + completed: function () { + return this.filterProperty('isCompleted', true).get('length'); + }.property('@each.isCompleted'), + + allAreDone: function (key, value) { + if (value === undefined) { + return !!this.get('length') && this.everyProperty('isCompleted', true); + } else { + this.setEach('isCompleted', value); + this.invoke('save') + return value; + } + }.property('@each.isCompleted') }); diff --git a/js/router.js b/js/router.js index c8e47fb..e555ad0 100644 --- a/js/router.js +++ b/js/router.js @@ -1,5 +1,9 @@ Todos.Router.map(function () { - this.resource('todos', { path: '/' }); + this.resource('todos', { path: '/' }, function () { + // additional child routes + this.route('active'); + this.route('completed'); + }); }); Todos.TodosRoute = Ember.Route.extend({ @@ -7,3 +11,31 @@ Todos.TodosRoute = Ember.Route.extend({ return this.store.find('todo'); } }); + +Todos.TodosIndexRoute = Ember.Route.extend({ + model: function () { + return this.modelFor('todos'); + } +}); + +Todos.TodosActiveRoute = Ember.Route.extend({ + model: function(){ + return this.store.filter('todo', function (todo) { + return !todo.get('isCompleted'); + }); + }, + renderTemplate: function(controller){ + this.render('todos/index', {controller: controller}); + } +}); + +Todos.TodosCompletedRoute = Ember.Route.extend({ + model: function(){ + return this.store.filter('todo', function (todo) { + return todo.get('isCompleted'); + }); + }, + renderTemplate: function(controller){ + this.render('todos/index', {controller: controller}); + } +}); diff --git a/js/views/edit_todo_view.js b/js/views/edit_todo_view.js new file mode 100644 index 0000000..8156654 --- /dev/null +++ b/js/views/edit_todo_view.js @@ -0,0 +1,7 @@ +Todos.EditTodoView = Ember.TextField.extend({ + didInsertElement: function () { + this.$().focus(); + } +}); + +Ember.Handlebars.helper('edit-todo', Todos.EditTodoView);