嘿,伙计们这里是我的js文件,我正在收集关于每个函数的错误消息:24,我不知道为什么我找不到什么错误。我只是想在console.log面板上看到项目列表,但它甚至没有在html页面上给我列表。
(function() {
window.App = {
Models: {},
Collections: {},
Views: {}
};
window.template = function(id){
return _.template( $('#' + id).html() );
};
App.Models.Task = Backbone.Model.extend({});
App.Collections.Task = Backbone.Collection.extend({
model: App.Models.Task
});
App.Views.Tasks = Backbone.View.extend({
tagName: 'ul',
render: function(){
this.collection.each( this.addOne, this);
return this;
},
addOne: function(task){
//creating new child view
var taskView = new App.Views.Task({ model: task });
//append to the root element
this.$el.append(taskView.render().el);
}
});
App.Views.Task = Backbone.View.extend({
tagName: 'li',
template: template('taskTemplate'),
events: {
'click .edit': 'editTask'
},
editTask: function(){
alert('you are editing the tas.');
},
render: function(){
var template = this.template( this.model.toJSON() );
this.$el.html(template);
return this;
}
});
var tasksCollection = new App.Views.Task([
{
title: 'Go to the store',
priority: 4
},
{
title: 'Go to the mall',
priority: 3
},
{
title: 'get to work',
priority: 5
}
]);
var tasksView = new App.Views.Tasks({ collection: tasksCollection });
$('.tasks').html(tasksView.render().el);
})();
答案 0 :(得分:1)
您正在创建一个视图实例,就好像它是一个类:
App.Views.Tasks = Backbone.View.extend({ /* ... */ });
var tasksCollection = new App.Views.Task([
{
title: 'Go to the store',
priority: 4
},
//...
然后你创建该视图的另一个实例并将其交给tasksCollection
,就像它真的是一个集合一样:
var tasksView = new App.Views.Tasks({ collection: tasksCollection });
但是视图和集合是不同的东西,只有集合有each
方法(除非你在视图中添加each
)。
您想将tasksCollection
创建为App.Collections.Task
:
var tasksCollection = new App.Collections.Task([
{
title: 'Go to the store',
priority: 4
},
//...
答案 1 :(得分:0)
您好每个方法无法找到该集合。以及任务的单一任务
在这一行: 改变这个
var tasksCollection = new App.Views.Task([
TO,this:
var tasksCollection = new App.Collections.Tasks([