vue2/examples/todomvc/js/app.js

103 lines
3.1 KiB
JavaScript
Raw Normal View History

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