Skip to content

Commit 4d22363

Browse files
author
Evan You
committed
update todomvc example
1 parent e1229da commit 4d22363

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

examples/todomvc/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ <h1>todos</h1>
3939
class="toggle"
4040
type="checkbox"
4141
v-model="completed"
42-
v-on="change: toggleTodo(this)"
4342
>
4443
<label v-text="title" v-on="dblclick: editTodo(this)"></label>
4544
<button class="destroy" v-on="click: removeTodo(this)"></button>

examples/todomvc/js/app.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
filter: 'all'
3030
},
3131

32+
// ready hook, watch todos change for data persistence
33+
ready: function () {
34+
this.$watch('todos', function (todos) {
35+
todoStorage.save(todos);
36+
});
37+
},
38+
3239
// a custom directive to wait for the DOM to be updated
3340
// before focusing on the input field.
3441
// http://vuejs.org/guide/directives.html#Writing_a_Custom_Directive
@@ -64,7 +71,6 @@
6471
this.todos.forEach(function (todo) {
6572
todo.completed = value;
6673
});
67-
todoStorage.save();
6874
}
6975
}
7076
},
@@ -80,16 +86,10 @@
8086
}
8187
this.todos.push({ title: value, completed: false });
8288
this.newTodo = '';
83-
todoStorage.save();
8489
},
8590

8691
removeTodo: function (todo) {
8792
this.todos.$remove(todo.$data);
88-
todoStorage.save();
89-
},
90-
91-
toggleTodo: function (todo) {
92-
todoStorage.save();
9393
},
9494

9595
editTodo: function (todo) {
@@ -106,7 +106,6 @@
106106
if (!todo.title) {
107107
this.removeTodo(todo);
108108
}
109-
todoStorage.save();
110109
},
111110

112111
cancelEdit: function (todo) {
@@ -116,7 +115,6 @@
116115

117116
removeCompleted: function () {
118117
this.todos = this.todos.filter(filters.active);
119-
todoStorage.save();
120118
}
121119
}
122120
});

examples/todomvc/js/store.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
'use strict';
66

77
var STORAGE_KEY = 'todos-vuejs';
8-
var todos = null;
98

109
exports.todoStorage = {
1110
fetch: function () {
12-
if (!todos) {
13-
todos = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
14-
}
15-
return todos;
11+
return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
1612
},
17-
save: function () {
13+
save: function (todos) {
1814
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos));
1915
}
2016
};

0 commit comments

Comments
 (0)