The results of the parse method of the Backbone.Model method are passed to the set method, which sets the attributes of the model. The point of the confusion for you, I think, is that the model identifier is not one of its attributes; This is one of its properties.
So what happens is:
- Your original data is returned from the server and passed to
parse - The same source data, supplemented by the
id attribute, is passed to set set looks like your idAttribute ( [ 'personId', 'jobId' ] ) and all the keys in the source data- Since none of these keys matches
idAttribute , none of them are used as the model identifier, and therefore you get your problem.
Your solution to setting this.id inside parse works, but it may cause problems in the future, because the parsing is usually designed to work with it (source data), and not to change the model itself; this part should happen next when set is called. Instead, a cleaner solution would be to do something like the following:
app.Assignment = Backbone.Model.extend({ // note that no idAttribute is specified, leaving it as the default "id" parse : function(resp) { resp.id = resp.personId + "_" + resp.jobId; return resp; } }
Or if you need another id ...
app.Assignment = Backbone.Model.extend({ idAttribute: 'personAndJobId', parse : function(resp) { resp.personAndJobId = resp.personId + "_" + resp.jobId; return resp; } }
machineghost
source share