Egemenk
Egemenk

Reputation: 307

Backbone.JS design pattern to fetch collections in order

I have several collections to be fetched on page load. Every collection depends on the previously loaded collection so I need to fetch them in order. This doesn't seem to be the right way to do it:

groupCollection.fetch({
    success: function() {
        userCollection.fetch({
            success: function() {
                itemCollection.fetch();    
            }
        })
    }
});

Is there a design pattern for this or am I taking a wrong approach from the beginning?

Upvotes: 1

Views: 334

Answers (1)

Andrey Kuzmin
Andrey Kuzmin

Reputation: 4479

There is an option to listen to reset event on parent collection, and load nested collection.

groupCollection.on("reset", function() {
  userCollection.fetch();
});

userCollection.on("reset", function() {
  itemCollection.fetch(); 
});

groupCollection.fetch();

Upvotes: 7

Related Questions