"this.collection.each不是函数;.应该'它不是简单地说“;每个”;
"this.collection.each is not a function". Shouldn't it simply say "each"?
我真的有一个由两部分组成的问题。
控制台告诉我:"类型错误:this.collection.each不是函数"
短期内,我很想知道为什么我的代码不起作用。
从长远来看,我更感兴趣的是知道为什么它没有告诉我"each"不是一个函数,因为这是我试图调用的方法。
附言:我已经确认JQuery加载正确,并且在加载此代码之前,所以这不是问题所在。
相关的javascript是:
$(function(){
var items = [
{ name: 'Abe Lincoln', details: 'Is he that guy from that new reality show?'},
{ name: 'Captain Planet', details: 'He is our hero'},
{ name: 'Karthus', details: 'Press R'},
{ name: 'Your Mom', details: 'She misses me'},
{ name: 'Teddy Roosevelt', details: 'Makes the most interesting man in the world look boring'}
];
var itemsCollectionView = new ListView({collection: items});
Backbone.history.start();
});
var ListView = Backbone.View.extend({
el: '#the-list',
initialize: function(){
this.render();
},
render: function(){
this.collection.each(function(model){
this.addOne(model);
}, this);
},
//create an itemview for a model, and add it to the list view
addOne:function(model){
var itemView = new ItemView({model: model});
this.$el.append(itemView.render().el);
}
});
this.collection.each
可以与Backbone一起使用,问题是您没有将实际的Backbone集合传递给ItemView的实例,而只是传递一个数组。您需要以下内容:
var itemsCollection = new ItemsCollection(items), // substitute your collection variable
itemsCollectionView = new ListView({ collection: itemsCollection });
此外,我尝试在Backbone 1.0.0和jQuery 1.10.1上运行您的代码,得到了
TypeError: Object [object Array] has no method 'each'
相关文章:
- Javascript,访问一个主要对象模块模式中的每个对象
- 简单的javascript在Shopify中不起作用
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 如何包含特定于每个视图angularjs的javascript文件
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何制作简单的php'在Javascript中的foreach等价物
- 使用简单的JavaScript或jQuery单独识别每个选择选项,并在DIV中显示
- 简单模式弹出窗口显示每个会话一次
- "this.collection.each不是函数;.应该'它不是简单地说“;每个”;
- 每个循环的简单谷歌电子表格
- HTML、PHP、JavaScript——如何为数据库中的每个单独记录制作一个按钮?看起来很简单,但事实并非如此;不起作
- 有没有一种简单的方法可以在不接触每个元素的情况下添加struts 1.3 html styleId属性
- 如何让浏览器自动访问1000个页面,并在每个页面上触发一个简单的javascript函数?
- 简单的jquery每个循环使浏览器挂起
- 简单嵌套的车把每个都不工作
- 如何使用Javascript和Jquery分别验证通过PHP循环创建的每个简单HTML单选表单
- Firebase简单登录+Angular-每个用户都有自己的数据
- 向Javascript中的每个元素添加简单事件
- 如何为每个正确答案制作一个简单的图表