The results of the parse
method of a Backbone.Model are passed to the set
method, which sets the attributes of the model. The point of confusion for you I think is that the model's ID isn't one of it's attributes; it's one of its properties.
So, what happens is this:
- Your raw data comes back from the server and is passed to
parse
- That same raw data, now augmented with a
id
attribute, is passed to set
set
looks and your idAttribute
([ 'personId', 'jobId' ]
) and all of the keys in the raw data
- Since none of those keys match the
idAttribute
, none of them are used as the model's ID, and thus you get your problem.
Your solution of setting this.id
inside parse
works, but it might cause problems down the road because parse is generally designed to operate on it's input (the raw data), not to modify the model itself; that part is supposed to happen next when set
is called. A cleaner solution would instead 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 want a different ID attribute ...
app.Assignment = Backbone.Model.extend({
idAttribute: 'personAndJobId',
parse : function(resp) {
resp.personAndJobId = resp.personId + "_" + resp.jobId;
return resp;
}
}
idAttribute
as an array is not valid. Every model must have one id field. What do your resource URLs look like? – Bobbyebobbysocks