bobo2000
bobo2000

Reputation: 1877

backbone.js - listing models contained in a collection

Tried the following:

var collectionList = users.fetch();
alert(collectionList);

This returns null despite there being models in it.

Update - this worked for me:

users.fetch({
    success: function() {
        console.log(users.toJSON());
    },
    error: function() {
        // something is wrong..
    }
});

Upvotes: 0

Views: 90

Answers (4)

Nurdin
Nurdin

Reputation: 23893

This one also can

var that = this;
users.fetch({
    success: function(collection, response) {
        console.log(that.collection.toJSON());
    },
    error: function() {
        // something is wrong..
    }
});

Upvotes: 0

users.fetch({
    success: function(response) {
        _.each(response.models, function(model) {
            //Do something with the model here
        });
    }
});

Upvotes: 1

bobo2000
bobo2000

Reputation: 1877

Fetch is an async method, so is empty upon being called, after you specify success and error you should be able to then list your models.

users.fetch({
    success: function() {
        console.log(users.toJSON());
    },
    error: function() {
        // something is wrong..
    }
});

Upvotes: 0

jgillich
jgillich

Reputation: 76219

users.fetch().then(function () {
    console.log(users.toJSON());
});

http://backbonejs.org/#Collection-fetch

Upvotes: 0

Related Questions