Created
October 16, 2017 13:34
-
-
Save scips/344fd928616551882217473872bd588e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://unpkg.com/vue"></script> | |
<style> | |
.done { | |
text-decoration: line-through; | |
} | |
</style> | |
<div id="app"> | |
<p> | |
<label v-for="m in modes"> | |
<input type="radio" :value="m" v-model="mode"> {{ m }} | |
</label> | |
</p> | |
<input | |
v-model="newTodo" | |
@keyup.enter="addNewTodo"> | |
<ul> | |
<li v-for="todo in filteredTodos" | |
:class="{ done: todo.done }" | |
@click="toggleTodo(todo)"> | |
{{ todo.text }} | |
</li> | |
</ul> | |
</div> | |
<script> | |
const vm = new Vue({ | |
data: { | |
modes: ['all', 'done', 'not done'], | |
newTodo: '', | |
// Source state | |
mode: 'all', | |
todos: [ | |
{ text: 'Learn JavaScript', done: true }, | |
{ text: 'Learn Vue', done: false } | |
] | |
}, | |
computed: { | |
// Derived state | |
filteredTodos () { | |
switch (this.mode) { | |
case 'done': | |
return this.todos.filter(todo => todo.done) | |
case 'not done': | |
return this.todos.filter(todo => !todo.done) | |
default: | |
return this.todos | |
} | |
} | |
}, | |
methods: { | |
toggleTodo (todo) { | |
todo.done = !todo.done | |
}, | |
addNewTodo () { | |
const newTodo = { | |
text: this.newTodo, | |
done: false | |
} | |
this.todos.push(newTodo) | |
this.newTodo = '' | |
} | |
} | |
}).$mount('#app') | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment