我有一个骨干的集合,获取一些数据,但是当我尝试使用各种方法在集合迭代它得到骨干收集不有没有这样的方法各有
Object [object Array] has no method 'each'
下面是收集的模型:
define (['backbone'],function(Backbone){
return Backbone.Model.extend({
url:'http://some_url/api/post/',
});
});//end define
集合本身: '
define (['backbone','models/Post'],function(Backbone,Post){
return Backbone.Collection.extend ({
model: Post,
initialize: function (model)
{
this.userId = model.userId;
this.start = model.start;
},
url: function()
{
return "http://some_url.com/api/post?userid=" + this.userId + "&start=" + this.start;
},
parse: function (response)
{
return response;
}
});
});
这是实例使用集合视图中的呼叫:
cookieVal = document.cookie.split ("; ");
posts = new PostCol ({userId:id = cookieVal[0].split('=')[1],start:1});
posts.fetch().success (function (response){
post = new Post({collection:response});
post.render();
console.log (post.el);
});
和视图的实现,它使用的收集:
define (['jquery','underscore','backbone','views/Actor'],function($,_,Backbone,Actor){
return Backbone.View.extend ({
className:'moment mb30',
render: function()
{
console.log (this.collection);
this.collection.each (function (model){
actor = new Actor ({model:model});
this.$el.append (actor.render().el);
},this);
return this;
}
});
});
为什么不工作的每个方法和哪能解决这个问题。
行'console.log(this.collection);'print?你把'this.collection'分配给了一个'Backone.Collection'吗? – Howard
它打印[对象,对象,对象,对象] – user2054833