我有一個骨幹的集合,獲取一些數據,但是當我嘗試使用各種方法在集合迭代它得到骨幹收集不有沒有這樣的方法各有
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