在集合中添加集合

Add collection in a collection

本文关键字:集合 添加      更新时间:2023-09-26

我创建了一个竞争者集合,它是另一个集合的旁路。在这个once(竞争者)中,我获取了另一个集合,我想将获取结果添加到第一个竞争者集合中。我知道存在选项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);
}
 );

这里的问题是"this"-作用域,它不指向任何东西。org通知这确实是一个异步函数,所以你必须给success-callback一些东西,以便在它被调用时引用。你可以这样做:

var self = this;
---
this.TweetsCollection.fetch({
    success: function (tweets) {
        self.add(tweets);
    }    
});

现在self指向内部函数的调用者