在集合中添加集合

时间:2014-05-07 19:47:50

标签: javascript backbone.js

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

}
 );

1 个答案:

答案 0 :(得分:1)

这里的问题是“这个”-scope,它没有引用任何东西。 Backbonejs.org通知这确实是一个异步函数,所以你必须给成功调用一些东西来调用它。你可以这样做:

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

现在自我指的是内在功能的来电者。

相关问题