vue2/examples/todomvc/js/app.js

108 lines
3.0 KiB
JavaScript
Raw Normal View History

2013-08-14 06:53:29 +08:00
var filters = {
all: function () { return true },
active: function (todo) { return !todo.completed },
completed: function (todo) { return todo.completed }
}
2013-08-14 02:34:51 +08:00
window.addEventListener('hashchange', function () {
Seed.broadcast('filterchange')
})
2013-08-14 06:53:29 +08:00
Seed.controller('todos', {
2013-08-07 14:03:16 +08:00
2013-08-14 06:53:29 +08:00
// initializer, reserved
init: function () {
window.app = this
2013-08-14 06:53:29 +08:00
// listen for hashtag change
this.updateFilter()
this.$on('filterchange', this.updateFilter.bind(this))
// instance properties
this.todos = todoStorage.fetch()
this.remaining = this.todos.filter(filters.active).length
},
2013-08-07 14:03:16 +08:00
// computed properties ----------------------------------------------------
2013-08-14 06:53:29 +08:00
total: {get: function () {
return this.todos.length
}},
2013-08-07 14:03:16 +08:00
2013-08-14 06:53:29 +08:00
completed: {get: function () {
return this.total - this.remaining
}},
2013-08-07 14:03:16 +08:00
// dynamic context computed property using info from target viewmodel
2013-08-14 06:53:29 +08:00
todoFiltered: {get: function (ctx) {
return filters[this.filter]({ completed: ctx.vm.completed })
2013-08-14 06:53:29 +08:00
}},
2013-08-11 23:30:53 +08:00
// dynamic context computed property using info from target element
2013-08-14 06:53:29 +08:00
filterSelected: {get: function (ctx) {
return this.filter === ctx.el.textContent.toLowerCase()
}},
// two-way computed property with both getter and setter
2013-08-14 06:53:29 +08:00
allDone: {
get: function () {
2013-08-14 06:53:29 +08:00
return this.remaining === 0
},
set: function (value) {
2013-08-14 06:53:29 +08:00
this.todos.forEach(function (todo) {
2013-08-10 19:56:59 +08:00
todo.completed = value
})
this.remaining = value ? 0 : this.total
todoStorage.save(this.todos)
}
2013-08-14 06:53:29 +08:00
},
2013-08-07 14:03:16 +08:00
// event handlers ---------------------------------------------------------
2013-08-14 06:53:29 +08:00
addTodo: function () {
var value = this.newTodo && this.newTodo.trim()
2013-08-11 23:30:53 +08:00
if (value) {
2013-08-14 06:53:29 +08:00
this.todos.unshift({ title: value, completed: false })
this.newTodo = ''
this.remaining++
todoStorage.save(this.todos)
2013-08-07 14:03:16 +08:00
}
2013-08-14 06:53:29 +08:00
},
2013-08-07 14:03:16 +08:00
2013-08-14 06:53:29 +08:00
removeTodo: function (e) {
this.todos.remove(e.vm)
this.remaining -= e.vm.completed ? 0 : 1
2013-08-14 06:53:29 +08:00
todoStorage.save(this.todos)
},
2013-08-07 14:03:16 +08:00
2013-08-14 06:53:29 +08:00
toggleTodo: function (e) {
this.remaining += e.vm.completed ? -1 : 1
2013-08-14 06:53:29 +08:00
todoStorage.save(this.todos)
},
2013-08-07 14:03:16 +08:00
2013-08-14 06:53:29 +08:00
editTodo: function (e) {
this.beforeEditCache = e.vm.title
e.vm.editing = true
2013-08-14 06:53:29 +08:00
},
2013-08-07 14:03:16 +08:00
2013-08-14 06:53:29 +08:00
doneEdit: function (e) {
if (!e.vm.editing) return
e.vm.editing = false
e.vm.title = e.vm.title.trim()
if (!e.vm.title) this.removeTodo(e)
2013-08-14 06:53:29 +08:00
todoStorage.save(this.todos)
},
2013-08-14 06:53:29 +08:00
cancelEdit: function (e) {
e.vm.editing = false
e.vm.title = this.beforeEditCache
2013-08-14 06:53:29 +08:00
},
removeCompleted: function () {
this.todos = this.todos.filter(filters.active)
todoStorage.save(this.todos)
},
2013-08-07 14:03:16 +08:00
2013-08-14 06:53:29 +08:00
updateFilter: function () {
var filter = location.hash.slice(2)
this.filter = (filter in filters) ? filter : 'all'
2013-08-07 14:03:16 +08:00
}
})
Seed.bootstrap()