The following code makes it look as if the "reset" event on the AccItemList collection is firing before the fetch is complete. The output to the console for this is as follows...
rendering
0
fetched
0
... so "render" is called before there are any models in the collection, which is clearly wrong, but just as confusingly even in the fetch success callback there don't seem to be any models in the collection. I have also tried instantiating the AccItemList collection in the router initializer, but that didn't make any different. I'm sure I'm missing something fundamental, please help, this is driving me crazy!
$(function () {
var AccItem = Backbone.Model.extend({
defaults: {}
});
var AccItemList = Backbone.Collection.extend({
model: AccItem,
url: "/Dashboard/Accommodation",
parse: function (response) {
this.add(response.AccItems);
}
});
var AccListView = Backbone.View.extend({
el: $("#temp"),
initialize: function () {
_.bindAll(this, 'render', 'renderAccItem');
this.collection = new AccItemList();
this.collection.on('reset', this.render);
var that = this;
that.collection.fetch({
success: function () {
console.log("fetched");
console.log(that.collection.models.length);
}
});
},
render: function () {
var that = this;
console.log("rendering");
console.log(this.collection.models.length);
}
});
var App = Backbone.Router.extend({
routes: {
"": "index"
},
index: function () {
},
init: function () {
var accItemView = new AccListView();
}
});
var app = new App();
app.init();
});