vue todos
todos
// , , vlaue
var STORAGE_KEY = 'todos'
var todoStorage = {
fetch: funciton (){
//
var todos = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
todos.forEach(function (todo, index){ // todo
todo.id = index
//todo index --id 0 1 2 3
//:key="todo.id"
})
todoStorage.uid = todos.length
return todos
},
// todos localStorage, string
save: function (todos){
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
}
}
//
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
})
}
}
var app = new Vue({
data: {
//
todos: todoStorage.fetch(),
//
newTodo: '',
//
visibility: 'all',
editedTodos: null
},
// data
// todos , , todos
watch: {
todos:{
// todos , ,
handler: function(todos){
todoStorage.save(todos)
},
deep: true
}
},
computed: {
filteredTodos: function() {
return filters[this.visibility](this.todos)
},
remianing: function() {
return filters.active(this.todos).length
},
allDone: function() {
get: function() {
return this.remiaing === 0
},
set: function() {
this.todos.forEach(function(todo){
//??????? value ---v-model data comple
todo.completed = value
})
}
}
},
// item
filters: {
pluralize: function(n) {
return n=== 1 ? 'item': 'items'
}
},
methods: {
//
addTodo : function(){
var value = this.newTodo && this.newTodo.trim()
// : return
if(!value){
return
}
// , todos
this.todos.push({
id: todoStorage.uid++,
title: value,
completed: false
})
// ,
this.newTodo = ''
},
// x, li
removeTodo: function(todo) {
// splice(2,1) 2
//indexOf()
this.todos.splice(this.todos.splice(todo),1)
},
editTodo: function(todo) {
},
doneTodo: function(todo) {
if(!this.editedTodo){
return
}
this.editedTodo = null
},
// esc
cancelEdit: function() {
},
//
removeCompleted: function() {
}
//
editTodo: function (todo) {
this.beforeEditCache = todo.title
this.editedTodo = todo
},
// ???? , ( ) enter
doneEdit: function (todo) {
if (!this.editedTodo) {
return
}
this.editedTodo = null
todo.title = todo.title.trim()
if (!todo.title) {
this.removeTodo(todo)
}
},
// esc
cancelEdit: function (todo) {
this.editedTodo = null
todo.title = this.beforeEditCache
},
//
removeCompleted: function () {
this.todos = filters.active(this.todos)
}
}
})
//
function onHashChange() {
// , url hash
// hash === all actived completed
var visibility = window.location.hash.replace(/#\/?/, '')
//hash , all actived completed , vue data-visibility
if(filters[visibility]){
app.visibility = visibility
}else {
// hash , all
window.location.hash = ''
app.visibility = 'all'
}
}
//
// value ,
directives: {
'todo-focus': function (el, binding) {
if (binding.value) {
el.focus()
}
}
}
})
// hashchange , onHashChange
window.addEventListener('hashchange', onHashChange)
onHashChange()
app.$mount('.todoapp')
[v-cloak] { display: none;}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.