PaulUithol/Backbone-relational — pull request #498 by ppecio (2014-10-28 14:38:28+00:00)
Title: Update backbone-relational.js

Assume, that I have model Zoo with relation to animals. If I use save() method with wait set to 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 on value = 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 initialised 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.