| 
									
										
										
										
											2013-12-08 06:29:17 +08:00
										 |  |  | var app = new Vue({ | 
					
						
							| 
									
										
										
										
											2013-10-12 06:13:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-22 00:58:42 +08:00
										 |  |  |     // the root element that will be compiled
 | 
					
						
							| 
									
										
										
										
											2013-10-12 06:13:57 +08:00
										 |  |  |     el: '#todoapp', | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-22 00:58:42 +08:00
										 |  |  |     // a custom directive to wait for the DOM to be updated
 | 
					
						
							|  |  |  |     // before focusing on the input field.
 | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |     directives: { | 
					
						
							|  |  |  |         'todo-focus': function (value) { | 
					
						
							|  |  |  |             if (value) { | 
					
						
							|  |  |  |                 var el = this.el | 
					
						
							|  |  |  |                 setTimeout(function () { el.focus() }, 0) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-22 00:58:42 +08:00
										 |  |  |     // the `created` lifecycle hook.
 | 
					
						
							|  |  |  |     // it will be called when the ViewModel instance is created.
 | 
					
						
							| 
									
										
										
										
											2013-12-17 14:38:01 +08:00
										 |  |  |     created: function () { | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |         this.filters = { | 
					
						
							|  |  |  |             all: function (todo) { todo.completed; return true }, | 
					
						
							|  |  |  |             active: function (todo) { return !todo.completed }, | 
					
						
							|  |  |  |             completed: function (todo) { return todo.completed } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-08-16 00:26:13 +08:00
										 |  |  |         this.updateFilter() | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |         window.addEventListener('hashchange', function () { | 
					
						
							|  |  |  |             app.updateFilter() | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2013-12-05 07:39:13 +08:00
										 |  |  |         this.remaining = this.todos.filter(function (todo) { | 
					
						
							|  |  |  |             return !todo.completed | 
					
						
							|  |  |  |         }).length | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-22 00:58:42 +08:00
										 |  |  |     // data
 | 
					
						
							| 
									
										
										
										
											2013-12-24 03:47:51 +08:00
										 |  |  |     data: { | 
					
						
							| 
									
										
										
										
											2014-01-22 00:58:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // fetch the saved todos from localStorage
 | 
					
						
							| 
									
										
										
										
											2013-10-12 06:13:57 +08:00
										 |  |  |         todos: todoStorage.fetch(), | 
					
						
							| 
									
										
										
										
											2014-01-22 00:58:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // a computed property with custom getter/setter
 | 
					
						
							| 
									
										
										
										
											2013-12-24 03:47:51 +08:00
										 |  |  |         allDone: { | 
					
						
							|  |  |  |             $get: function () { | 
					
						
							|  |  |  |                 return this.remaining === 0 | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             $set: function (value) { | 
					
						
							|  |  |  |                 this.todos.forEach(function (todo) { | 
					
						
							|  |  |  |                     todo.completed = value | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |                 this.remaining = value ? 0 : this.todos.length | 
					
						
							|  |  |  |                 todoStorage.save() | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-22 00:58:42 +08:00
										 |  |  |     // methods that implement data logic.
 | 
					
						
							|  |  |  |     // note there's no DOM manipulation here at all!
 | 
					
						
							| 
									
										
										
										
											2013-12-24 03:47:51 +08:00
										 |  |  |     methods: { | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |         updateFilter: function () { | 
					
						
							|  |  |  |             var filter = location.hash.slice(2) | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |             this.filter = (filter in this.filters) ? filter : 'all' | 
					
						
							|  |  |  |             this.filterTodo = this.filters[this.filter] | 
					
						
							| 
									
										
										
										
											2013-08-10 00:18:34 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2013-08-09 04:24:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |         addTodo: function () { | 
					
						
							|  |  |  |             var value = this.newTodo && this.newTodo.trim() | 
					
						
							|  |  |  |             if (value) { | 
					
						
							|  |  |  |                 this.todos.unshift({ title: value, completed: false }) | 
					
						
							|  |  |  |                 this.newTodo = '' | 
					
						
							|  |  |  |                 this.remaining++ | 
					
						
							| 
									
										
										
										
											2013-08-24 02:51:49 +08:00
										 |  |  |                 todoStorage.save() | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |         removeTodo: function (todo) { | 
					
						
							|  |  |  |             this.todos.remove(todo.$data) | 
					
						
							|  |  |  |             this.remaining -= todo.completed ? 0 : 1 | 
					
						
							| 
									
										
										
										
											2013-08-24 02:51:49 +08:00
										 |  |  |             todoStorage.save() | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |         toggleTodo: function (todo) { | 
					
						
							|  |  |  |             this.remaining += todo.completed ? -1 : 1 | 
					
						
							| 
									
										
										
										
											2013-08-24 02:51:49 +08:00
										 |  |  |             todoStorage.save() | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |         editTodo: function (todo) { | 
					
						
							|  |  |  |             this.beforeEditCache = todo.title | 
					
						
							|  |  |  |             this.editedTodo = todo | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |         doneEdit: function (todo) { | 
					
						
							| 
									
										
										
										
											2013-08-26 02:16:13 +08:00
										 |  |  |             if (!this.editedTodo) return | 
					
						
							|  |  |  |             this.editedTodo = null | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |             todo.title = todo.title.trim() | 
					
						
							|  |  |  |             if (!todo.title) this.removeTodo(todo) | 
					
						
							| 
									
										
										
										
											2013-08-24 02:51:49 +08:00
										 |  |  |             todoStorage.save() | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |         cancelEdit: function (todo) { | 
					
						
							| 
									
										
										
										
											2013-08-26 02:16:13 +08:00
										 |  |  |             this.editedTodo = null | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |             todo.title = this.beforeEditCache | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2014-01-19 11:42:55 +08:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |         removeCompleted: function () { | 
					
						
							| 
									
										
										
										
											2013-12-05 09:17:38 +08:00
										 |  |  |             this.todos.remove(function (todo) { | 
					
						
							|  |  |  |                 return todo.completed | 
					
						
							| 
									
										
										
										
											2013-12-05 07:39:13 +08:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											2013-08-24 02:51:49 +08:00
										 |  |  |             todoStorage.save() | 
					
						
							| 
									
										
										
										
											2013-08-15 12:39:32 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-08-16 00:26:13 +08:00
										 |  |  | }) |