Question:
What is the lifecycle of the standard fetch method for a collection in Backbone? i.e. what events/methods are fired and in what order?
Context:
The JSON response I receive from the server for my collection has an array of models and a property:
{
results: [model1, model2],
aProperty: "example"
}
I would like to read this property from the JSON response and set it as a property on the Collection. I am currently overriding the parse function:
parse: function(response, options) {
this.aProperty = response.aProperty;
return response.results;
}
This feels like the wrong place to set properties in the collection - the parse function has a specific job and happens before the model array has been verified.
I have also tried:
initialize: function() {
this.on('sync', function(collection, resp) {
collection.aProperty = resp.aProperty;
});
}
However, 'sync' is called after the success callback for a fetch (I need to set the properties as part of fetch, before the success callback).