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.
Assume, that I have model Zoo with relation to animals. If I use save() method with
wait: true
to update & save the model and provide new animals as a list of IDs (like{animals: [1,23,43]}
), toJSON method in backbone-relations.js fails onvalue = value.concat(rel.keyIds)
, because value is null.This is because Backbone does not use set() method in save() if wait is True - it's only make a simple copy of provided attributes and set as model's attributes. The relation (that is an attribute of course) is not initialized so is not a model (for HasOne relation), or Collection (for HasMany relation), but contains simple value like Number, String or Array (depends on the type of relation and the data I set), which are keys for relation.
This pull request fix this bug and add additional check to toJSON method. If relation value is not a model or collection and is not empty assume that is a raw data and should be returned "as is". toJSON method returns list of related object keys, I used a related object keys in attributes object for Backbone.save() method, so everything is OK.