I am trying to understand Backbone and tried to create some small REST API fronted, but can not get View
to work.
fetch()
returns valid JSON array of three elements. And this.collection.models
is not empty (typeof object
- []
) - it has tree child object elements in it. But each
iteration doesn't fire.
When check if collection.models exists with console.log(this.collection.models);
it looks like all is right:
I would be thankful for any advice!
var Account = Backbone.Model.extend({});
var AccountsCollection = Backbone.Collection.extend({
model: Account,
url: 'api/v1/accounts',
initialize: function () {
this.fetch();
}
});
var AccountsView = Backbone.View.extend({
render: function() {
// Check if there is something
console.log(this.collection.models);
// This doesn't work
_.each(this.collection.models, function(model) {
console.log(model);
});
// Neither this
this.collection.each(function(model) {
console.log(model);
});
}
});
var a = new AccountsView({collection: new AccountsCollection()});
a.render();