vue2/examples/todomvc/js/app.js

132 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-04-16 04:20:35 +08:00
/*global Vue, todoStorage */
(function (exports) {
'use strict';
var filters = {
all: function (todos) {
return todos;
},
active: function (todos) {
return todos.filter(function (todo) {
return !todo.completed;
});
},
completed: function (todos) {
return todos.filter(function (todo) {
return todo.completed;
});
}
};
exports.app = new Vue({
// the root element that will be compiled
el: '.todoapp',
// app initial state
data: {
todos: todoStorage.fetch(),
newTodo: '',
editedTodo: null,
visibility: 'all'
},
// watch todos change for localStorage persistence
watch: {
todos: {
handler: function (todos) {
todoStorage.save(todos);
},
deep: true
}
},
// computed properties
// http://vuejs.org/guide/computed.html
computed: {
filteredTodos: function () {
return filters[this.visibility](this.todos);
},
remaining: function () {
return filters.active(this.todos).length;
},
allDone: {
get: function () {
return this.remaining === 0;
},
set: function (value) {
this.todos.forEach(function (todo) {
todo.completed = value;
});
}
}
},
2016-04-30 07:40:53 +08:00
filters: {
2016-04-16 04:20:35 +08:00
pluralize: function (n) {
return n === 1 ? 'item' : 'items'
2016-04-30 07:40:53 +08:00
}
},
2016-04-16 04:20:35 +08:00
2016-04-30 07:40:53 +08:00
// methods that implement data logic.
// note there's no DOM manipulation here at all.
methods: {
2016-04-16 04:20:35 +08:00
addTodo: function () {
var value = this.newTodo && this.newTodo.trim();
if (!value) {
return;
}
this.todos.push({
id: todoStorage.uid++,
title: value,
completed: false
});
2016-04-16 04:20:35 +08:00
this.newTodo = '';
},
removeTodo: function (todo) {
2016-04-21 18:29:02 +08:00
this.todos.splice(this.todos.indexOf(todo), 1);
2016-04-16 04:20:35 +08:00
},
editTodo: function (todo) {
this.beforeEditCache = todo.title;
this.editedTodo = todo;
},
doneEdit: function (todo) {
if (!this.editedTodo) {
return;
}
this.editedTodo = null;
todo.title = todo.title.trim();
if (!todo.title) {
this.removeTodo(todo);
}
},
cancelEdit: function (todo) {
this.editedTodo = null;
todo.title = this.beforeEditCache;
},
removeCompleted: function () {
this.todos = filters.active(this.todos);
}
},
// a custom directive to wait for the DOM to be updated
// before focusing on the input field.
// http://vuejs.org/guide/custom-directive.html
directives: {
'todo-focus': function (el, value) {
if (value) {
el.focus();
}
}
}
});
})(window);