Skip to content Skip to sidebar Skip to footer

Backbone.js Collection.models Not Showing, But There

I have a view that's doing a fetch() to a collection and returning some models from the server. ProductsView = Backbone.View.extend({ initialize: function() { _.bindAl

Solution 1:

In general this.collection.fetch({data: {limit : this.options.limit}}) is an asynchronous operation, so you next line is not necessarily going to print the correct content of the collection.

Instead you should use success and error callbacks the fetch method provides as part of its options parameter (or listen to collection's change event), like so:

this.collection.fetch(
    { 
        data: { limit : this.options.limit },
        success : function(collection, rawresponse) { 
            // do something with the data, like calling render 
        }
    }
);

For completness sake:

this.collection.on('change', function(){ // some handling of the data };this.collection.fetch();

is the event-based method.

Post a Comment for "Backbone.js Collection.models Not Showing, But There"