Call parse before retrieving idAttribute #198
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have an API that returns the object payload as follow:
{
result : {
id : 123
field1 : ...
}
}
I have a custom parse method on my models that essentially does 'return response.result'.
In the findOrCreate method, the idAttribute must be retrieved and
it uses item[ type.prototype.idAttribute ]. This is fine when the
API returns the id as a top level key but it brakes otherwise and
parse must be called first.
I have not had a chance to write a unit test yet. I am new to javascript and backbone and I wanted to get your feedback on the change before I invest more time implementing a test case. This fix solves my particular problem but it may have side effects I am not aware of. Without this fix, I would get 'Cannot instantiate more than one Backbone.RelationalModel with the same id per type! ' errors since findOrCreate would never be able to pull the model idAttribute.