Stefano Maglione
Stefano Maglione

Reputation: 4150

Add collection in a collection

I've created a contenitor collection that is a by-pass for another collection. Inside this once (contenitor) I fetch another collection and I want add the fetch results inside the first one contenitor collection.I know exists options add:true but seems not works.

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
      }
   });
 },
});

and this is the fetch of 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);

}
 );

Upvotes: 0

Views: 61

Answers (1)

Tohveli
Tohveli

Reputation: 485

Problem here is the "this"-scope, which doesn't refer to anything. Backbonejs.org informs that this indeed is an asyncronous function, so you have to give success-callback something to refer to when it is called. You could do this:

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

Now self refers to caller of inner function.

Upvotes: 1

Related Questions