@xiaoqq
2016-08-31T11:58:33.000000Z
字数 4781
阅读 1130
Vue
ToDoMVC是采用很多套MV* 框架来实现来实现一个固定规范的JavaScript Web App,从而方便我们对不同的框架进行学习和比较。
html
<section id="todoapp">
<header id="header">...
</header>
<section id="main">...
</section>
<footer id="footer">...
</footer>
</section>
<script type="text/ng-template" id="todomvc-index.html">
作为一个模板输出。Vue写成的ToDoMVC比我想象的要容易很多。
When there are no todos, #main
and #footer
should be hidden.
New todos are entered in the input at the top of the app. The input element should be focused when the page is loaded, preferably by using the autofocus
input attribute. Pressing Enter creates the todo, appends it to the todo list, and clears the input. Make sure to .trim()
the input and then check that it's not empty before creating a new todo.
Vue中对于时间的绑定直接使用@
符号,可以通过keyup来声明监听键盘事件
<!-- 新增任务 -->
<input type="text" id="new-todo" placeholder="待办事项" v-model="newTodo" autofocus @keyup.enter="addTodo">
//增加任务
methods: {
addTodo: function() {
if(!this.newTodo.trim()){
return;
}
this.todos.push({
title:this.newTodo,
completed:false
})
storage.saveData(this.todos);
},
...
}
This checkbox toggles all the todos to the same state as itself. Make sure to clear the checked state after the "Clear completed" button is clicked. The "Mark all as complete" checkbox should also be updated when single todo items are checked/unchecked. Eg. When all the todos are checked it should also get checked.
Vue中这里处理地相当“聪明”,直接声明computed
属性,并且使用get
和set
方法,当allChecked改变时执行相应的操作。
<!-- 全选/全不选 任务 -->
<input type="checkbox" id="toggle-all" v-model="allChecked">
computed:{
...
allChecked:{
get: function(){
return this.todoCount === 0;
},
set: function(newVal){
this.todos.forEach(function(todo){
todo.completed = newVal;
});
}
}
}
A todo item has three possible interactions:
Clicking the checkbox marks the todo as complete by updating its completed
value and toggling the class completed
on its parent <li>
Double-clicking the <label>
activates editing mode, by toggling the .editing
class on its <li>
Hovering over the todo shows the remove button (.destroy
)
<li v-for="todo in todos" :class="{completed: todo.completed, editing:todo === editTodo}">
<div class="view">
<input type="checkbox" class="toggle" v-model="todo.completed">
<label @dblclick="showEdit(todo)">{{todo.title}}</label>
<!-- 删除任务 -->
<button class="destroy" @click="deleteTodo($index)"></button>
</div>
<!-- 编辑任务 -->
<form @submit="saveEdit(todo)">
<input type="text" class="edit" @blur="saveEdit(todo)" @keyup.esc="cancel(todo)" v-model="todo.title" index="{{$index}}" todo-focus="todo === editTodo" todo-escape="revertEdit(todo)">
</form>
</li>
When editing mode is activated it will hide the other controls and bring forward an input that contains the todo title, which should be focused (.focus()
). The edit should be saved on both blur and enter, and the editing
class should be removed. Make sure to .trim()
the input and then check that it's not empty. If it's empty the todo should instead be destroyed. If escape is pressed during the edit, the edit state should be left and any changes be discarded.
定义一个指令
但是不明白,为什么focus事件需要放在$timeout中?
Vue中可以直接监听esc键
//指令监听ESC按键
<!-- 编辑任务 -->
<form @submit="saveEdit(todo)">
<input type="text" class="edit" @blur="saveEdit(todo)" @keyup.esc="cancel(todo)" v-model="todo.title" index="{{$index}}" todo-focus="todo === editTodo" todo-escape="revertEdit(todo)">
</form>
Displays the number of active todos in a pluralized form. Make sure the number is wrapped by a <strong>
tag. Also make sure to pluralize the item
word correctly: 0 items
, 1 item
, 2 items
. Example: 2 items left
Removes completed todos when clicked. Should be hidden when there are no completed todos.
清除完成项:
//清除完成任务
clearCompleted: function() {
this.todos = this.todos.filter(function(todo){
return !todo.completed;
});
storage.saveData(this.todos);
},
重点在于使用filter过滤出未完成的list
Your app should dynamically persist the todos to localStorage. If the framework has capabilities for persisting data (e.g. Backbone.sync), use that. Otherwise, use vanilla localStorage. If possible, use the keys id
, title
, completed
for each item. Make sure to use this format for the localStorage name: todos-[framework]
. Editing mode should not be persisted.
把调用localStorage的功能写在服务中,主要包含增删改查等功能。
//注册服务
var storage = {
STORAGE_ID: 'vue',
saveData: function saveData(newTodo) {
localStorage.setItem(this.STORAGE_ID, JSON.stringify(newTodo));
},
getData: function getData() {
return JSON.parse(localStorage.getItem(this.STORAGE_ID)) || [];
}
}
页面加载时从localStorage中读取事项:
Routing is required for all implementations. If supported by the framework, use its built-in capabilities. Otherwise, use the Flatiron Director routing library located in the /assets
folder. The following routes should be implemented: #/
(all - default), #/active
and #/completed
(#!/
is also allowed). When the route changes, the todo list should be filtered on a model level and the selected
class on the filter links should be toggled. When an item is updated while in a filtered state, it should be updated accordingly. E.g. if the filter is Active
and the item is checked, it should be hidden. Make sure the active filter is persisted on reload.
代码地址:
演示地址: