Skip to content

Commit

Permalink
simplify todomvc example
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 19, 2016
1 parent 8242d94 commit ccd8d58
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 188 deletions.
157 changes: 157 additions & 0 deletions examples/todomvc/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Full spec-compliant TodoMVC with localStorage persistence
// and hash-based routing in ~150 lines.

// localStorage persistence
var STORAGE_KEY = 'todos-vuejs-2.0'
var todoStorage = {
fetch: function () {
var todos = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
todos.forEach(function (todo, index) {
todo.id = index
})
todoStorage.uid = todos.length
return todos
},
save: function (todos) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
}
}

// visibility filters
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
})
}
}

// app Vue instance
var app = new Vue({
// 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
})
}
}
},

filters: {
pluralize: function (n) {
return n === 1 ? 'item' : 'items'
}
},

// methods that implement data logic.
// note there's no DOM manipulation here at all.
methods: {
addTodo: function () {
var value = this.newTodo && this.newTodo.trim()
if (!value) {
return
}
this.todos.push({
id: todoStorage.uid++,
title: value,
completed: false
})
this.newTodo = ''
},

removeTodo: function (todo) {
this.todos.splice(this.todos.indexOf(todo), 1)
},

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()
}
}
}
})

// handle routing
function onHashChange () {
var visibility = window.location.hash.replace(/#\/?/, '')
if (filters[visibility]) {
app.visibility = visibility
} else {
window.location.hash = ''
app.visibility = 'all'
}
}

window.addEventListener('hashchange', onHashChange)
onHashChange()

// mount
app.$mount('.todoapp')
9 changes: 4 additions & 5 deletions examples/todomvc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ <h1>todos</h1>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer>

<script src="../../dist/vue.js"></script>
<script src="js/store.js"></script>
<script src="js/app.js"></script>
<script src="js/routes.js"></script>
<script>
app.$mount('.todoapp')
// for testing
if (navigator.userAgent.indexOf('PhantomJS') > -1) localStorage.clear()
</script>
<script src="../../dist/vue.js"></script>
<script src="app.js"></script>
</body>
</html>
127 changes: 0 additions & 127 deletions examples/todomvc/js/app.js

This file was deleted.

24 changes: 0 additions & 24 deletions examples/todomvc/js/routes.js

This file was deleted.

23 changes: 0 additions & 23 deletions examples/todomvc/js/store.js

This file was deleted.

9 changes: 0 additions & 9 deletions examples/todomvc/package.json

This file was deleted.

0 comments on commit ccd8d58

Please sign in to comment.