我创建了一个容器集合,它是另一个集合的旁路。在这一次(contenitor)中,我获取另一个集合,并且我想将获取结果添加到第一个contenitor集合中。我知道存在选项 add:true 但似乎不起作用。
var Contenitor = Backbone.Collection.extend({
twitterQuery:undefined,
page: undefined,
initialize: function () {
this.TweetsCollection = new Tweets();
},
fetch: function() {
this.TweetsCollection.query=this.twitterQuery;
this.TweetsCollection.page=this.page;
this.TweetsCollection.fetch({
success:function (tweets){
this.add(tweets);<---here nothing is added to this collection
}
});
},
});
这是 TWeetsCollection 的获取:
fetch: function(options) {
var collection = this;
var params = {
user_id: this.query,
//screen_name:"brad"
page:this.page
};
cb.__call(
"statuses_userTimeline",
params,
function (reply) {
// console.log(reply);
collection.reset(reply);
}
);
最佳答案
问题是“这个” - scope,它没有指任何东西。 Backbonejs.org告知这确实是一个异构函数,因此您必须在调用它时给出成功的东西。您可以这样做:
var self = this;
---
this.TweetsCollection.fetch({
success: function (tweets) {
self.add(tweets);
}
});
现在 self 指的是内部功能的来电者。
关于javascript - 在集合中添加集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23526995/