Code of Collection Model and View
Backbone.Collection.extend({
model: xModel,
initialize: function(url) {
this.targetUrl = url;
},
url: function () {
return this.targetUrl;
},
parse: function (data) {
return data.instances;
}
}
Backbone.Model.extend({
defaults: {
location: '',
xyz: ''
},
initialize: function (options) {
},
url: function () {
return this.get('location');
}
}
}
renderInstances: function () {
this.collection = new xyzInstanceCollection(instPath);
this.collection.fetch({
success: _.bind(
function (data) {
for (var i = 0; i < this.collection.length; ++i) {
var model = this.collection.at(i);
model.fetch({
success: _.bind(
function (data) {
/*Call a subView to run the data over a template
and display the instance fetched*/
}, this),
error:
function (e, response) {
/*Do Something else*/
}
});
}
}, this),
error:
function (e, response) {
/*Do Something*/
}
}
});
}
Issue 1: Now I was expecting that the views of the instances that are called from the success callback will start rendering as soon as the fetch of that model gets the data, but what I see is Backbone.js first queues up all the ajax calls of the complete loop, then once all these calls have returned data , only then the views starts rendering,this happens irrespective of sync or async fetch, why is this so ?
Issue 2: If a refresh button is pressed before all models are fetched from this loop, the browser returns error in communication. How to abort these calls when user wants to route to another page without caring to see the results, there can be 1000s of xhr's returned from the loop of model fetches, I cannot collect them all and execute abort on them, is there any better way , please suggest.