Commit 40d9d467 authored by Evan You's avatar Evan You

update Vue implementation to use 0.10.0

parent e8f8532b
{ {
"name": "todomvc-vue", "name": "todomvc-vue",
"dependencies": { "dependencies": {
"vue": "~0.8.3", "vue": "~0.10.0",
"todomvc-common": "~0.1.9" "todomvc-common": "~0.1.9"
} }
} }
\ No newline at end of file
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<section id="main" v-show="todos.length"> <section id="main" v-show="todos.length">
<input id="toggle-all" type="checkbox" v-model="allDone"> <input id="toggle-all" type="checkbox" v-model="allDone">
<ul id="todo-list"> <ul id="todo-list">
<li class="todo" v-repeat="todos" v-if="filterTodo(this)" v-class="completed: completed, editing: this == editedTodo"> <li class="todo" v-repeat="todos | filterTodos" v-class="completed: completed, editing: this == editedTodo">
<div class="view"> <div class="view">
<input class="toggle" type="checkbox" v-model="completed" v-on="change: toggleTodo"> <input class="toggle" type="checkbox" v-model="completed" v-on="change: toggleTodo">
<label v-text="title" v-on="dblclick: editTodo(this)"></label> <label v-text="title" v-on="dblclick: editTodo(this)"></label>
......
...@@ -4,7 +4,19 @@ ...@@ -4,7 +4,19 @@
'use strict'; 'use strict';
exports.app = new Vue({ var filters = {
all: function () {
return true;
},
active: function (todo) {
return !todo.completed;
},
completed: function (todo) {
return todo.completed;
}
};
var app = exports.app = new Vue({
// the root element that will be compiled // the root element that will be compiled
el: '#todoapp', el: '#todoapp',
...@@ -13,7 +25,15 @@ ...@@ -13,7 +25,15 @@
data: { data: {
todos: todoStorage.fetch(), todos: todoStorage.fetch(),
newTodo: '', newTodo: '',
editedTodo: null editedTodo: null,
filter: 'all'
},
// ready hook, watch todos change for data persistence
ready: function () {
this.$watch('todos', function (todos) {
todoStorage.save(todos);
});
}, },
// a custom directive to wait for the DOM to be updated // a custom directive to wait for the DOM to be updated
...@@ -31,35 +51,17 @@ ...@@ -31,35 +51,17 @@
} }
}, },
// the `created` lifecycle hook. filters: {
// this is where we do the initialization work. filterTodos: function (todos) {
// http://vuejs.org/api/instantiation-options.html#created return todos.filter(filters[this.filter]);
created: function () { }
// setup filters
this.filters = {
all: function (todo) {
// collect dependency.
// http://vuejs.org/guide/computed.html#Dependency_Collection_Gotcha
/* jshint expr:true */
todo.completed;
return true;
},
active: function (todo) {
return !todo.completed;
},
completed: function (todo) {
return todo.completed;
}
};
// default filter
this.setFilter('all');
}, },
// computed property // computed property
// http://vuejs.org/guide/computed.html // http://vuejs.org/guide/computed.html
computed: { computed: {
remaining: function () { remaining: function () {
return this.todos.filter(this.filters.active).length; return this.todos.filter(filters.active).length;
}, },
allDone: { allDone: {
$get: function () { $get: function () {
...@@ -69,7 +71,6 @@ ...@@ -69,7 +71,6 @@
this.todos.forEach(function (todo) { this.todos.forEach(function (todo) {
todo.completed = value; todo.completed = value;
}); });
todoStorage.save();
} }
} }
}, },
...@@ -78,11 +79,6 @@ ...@@ -78,11 +79,6 @@
// note there's no DOM manipulation here at all. // note there's no DOM manipulation here at all.
methods: { methods: {
setFilter: function (filter) {
this.filter = filter;
this.filterTodo = this.filters[filter];
},
addTodo: function () { addTodo: function () {
var value = this.newTodo && this.newTodo.trim(); var value = this.newTodo && this.newTodo.trim();
if (!value) { if (!value) {
...@@ -90,16 +86,10 @@ ...@@ -90,16 +86,10 @@
} }
this.todos.push({ title: value, completed: false }); this.todos.push({ title: value, completed: false });
this.newTodo = ''; this.newTodo = '';
todoStorage.save();
}, },
removeTodo: function (todo) { removeTodo: function (todo) {
this.todos.remove(todo.$data); this.todos.$remove(todo.$data);
todoStorage.save();
},
toggleTodo: function () {
todoStorage.save();
}, },
editTodo: function (todo) { editTodo: function (todo) {
...@@ -116,7 +106,6 @@ ...@@ -116,7 +106,6 @@
if (!todo.title) { if (!todo.title) {
this.removeTodo(todo); this.removeTodo(todo);
} }
todoStorage.save();
}, },
cancelEdit: function (todo) { cancelEdit: function (todo) {
...@@ -125,12 +114,11 @@ ...@@ -125,12 +114,11 @@
}, },
removeCompleted: function () { removeCompleted: function () {
this.todos.remove(function (todo) { this.todos = this.todos.filter(filters.active);
return todo.completed;
});
todoStorage.save();
} }
} }
}); });
app.filters = filters;
})(window); })(window);
\ No newline at end of file
...@@ -8,10 +8,17 @@ ...@@ -8,10 +8,17 @@
Object.keys(app.filters).forEach(function (filter) { Object.keys(app.filters).forEach(function (filter) {
router.on(filter, function () { router.on(filter, function () {
app.setFilter(filter); app.filter = filter;
}); });
}); });
router.configure({
notfound: function () {
window.location.hash = '';
app.filter = 'all';
}
});
router.init(); router.init();
})(app, Router); })(app, Router);
\ No newline at end of file
...@@ -5,16 +5,12 @@ ...@@ -5,16 +5,12 @@
'use strict'; 'use strict';
var STORAGE_KEY = 'todos-vuejs'; var STORAGE_KEY = 'todos-vuejs';
var todos = null;
exports.todoStorage = { exports.todoStorage = {
fetch: function () { fetch: function () {
if (!todos) { return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
todos = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
}
return todos;
}, },
save: function () { save: function (todos) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos)); localStorage.setItem(STORAGE_KEY, JSON.stringify(todos));
} }
}; };
......
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