//存取保存的数据
var store = {
save(key,value){
localStorage.setItem(key,JSON.stringify(value));
},
fetch(key){
return JSON.parse(localStorage.getItem(key)) || [];
}
}
//取出store对象中key值为mylist的数据
var list = store.fetch("mylist");
//MVVM中的VM对象
var vm = new Vue({
el:".main",
data:{
list:list,
todo:‘‘,
edtorTodos:‘‘,
originalTitle:‘‘,
visibility:"all"//通过这个属性值的变化对数据进行筛选
},
methods:{
//添加数据
addTodo(){
this.list.push({
title:this.todo,
isChecked:false,
});
this.todo=‘‘;
},
//删除数据
deleteTodo(todo){
var index = this.list.indexOf(todo);
this.list.splice(index,1);
},
//编辑数据
edtorTodo(todo){
this.originalTitle=todo.title;
this.edtorTodos=todo;
},
//编辑完成
edtorTodoed(todo){
this.edtorTodos=‘‘;
},
//取消编辑
cancleTodo(todo){
todo.title=this.originalTitle;
this.edtorTodos=‘‘;
}
},
directives:{
"foucs":{
update(el,binding){
if(binding.value){
el.focus();
}
}
}
},
//计算属性
computed:{
unfinishedCount:function(){
//统计list中isChecked为false的项的个数
return this.list.filter(function(item){return !item.isChecked}).length;
},
//根据hash过滤后的数据
filteredList:function(){
var filter = {
all:function(list){
return list;
},
finished:function(list){
return list.filter(function(item){
return item.isChecked;
})
},
unfinished:function(list){
return list.filter(function(item){
return !item.isChecked;
})
}
};
return filter[this.visibility](list);
}
},
//监控,当list改变时,调用store的save方法
watch:{
list:{
handler:function(){
store.save("mylist",this.list);
},
deep:true //深层监控,保证存储到list对象中每一层如isChecked
}
}
});
function watchHashChange(){
var hash = window.location.hash.slice(1);
vm.visibility = hash;
console.log(hash);
}
watchHashChange();
window.addEventListener("hashchange",watchHashChange);
.big-title{
background-color:goldenrod;
}
.completed{
text-decoration:line-through
}
.todo > :not(:first-child) {
display: none;
}
.editing > :first-child{
display: none;
}
.editing > :not(:first-child) {
display: block;
}