我有以下代码,我昨天正在处理 但今天当我重新运行代码时,我收到以下错误: 未捕获的ReferenceError:tasksCollection未定义
(function(){
window.App={
Models:{},
Collections:{},
Views:{}
};
window.template=function(id){
return _.template($('#' +id).html());
};
App.Models.Task=Backbone.Model.extend({});
App.Collections.Tasks=Backbone.Collection.extend({
model:App.Models.Task
});
App.Views.Task=Backbone.View.extend({
tagName:'li',
template:template('taskTemplate'),
initialize:function(){
//when model change it will change !
//this.model.on('change',this.render,this);
this.render();
},
events:{
'click .edit':'editTask',
'click .delete':'deleteTask'
},
editTask:function(){
var newTitle = prompt('what would you like to be your title?',this.model.get('title'));
this.model.set('title',newTitle);
},
deleteTask:function(){
alert('delete');
},
render:function(){
var template= this.template(this.model.toJSON());
this.$el.html(template);
return this;
}
});
App.Views.Tasks=Backbone.View.extend({
tagName:'ul',
initialize:function(){
this.render();
},
render:function(){
this.collection.each(this.addOne,this);
return this;
},
addOne:function(task){
//creat new child view
var taskView= new App.Views.Task({ model:task});
taskView.render();
//append to the root element
this.$el.append(taskView.render().el);
}
});
var tasksCollection=new App.Collections.Tasks([
{
title:'Hi',
name:'samin'
},
{
title:'Hi',
name:'nazanin'
},
{
title:'Baby',
name:'nazanin'
}
]);
var tasksView=new App.Views.Tasks({collection:tasksCollection});
console.log(tasksView.el);
$('.tasks').html(tasksView.el);
})();
我无法解决错误。看起来一切都应该运作良好 你能帮助我解决这个问题吗?
答案 0 :(得分:-1)
经过一番研究后我就这么想了
我们应该使用 var 代替 var 来定义新的集合窗口。
如何? 这样 - >
window.tasksCollection=new App.Collections.Tasks([
{
title:'Hi',
name:'samin'
},
{
title:'Hi',
name:'nazanin'
},
{
title:'Baby',
name:'nazanin'
}
]);