Backbone-relational
Backbone-relational copied to clipboard
toJSON with sub relations models
Hello,
I met a particular case, I have a model that contains hasOne relationships which in themselves have same relationships.
The problem was that the toJSON took over the includeInJSON for the model but not for the related models
I propose to put this code in the toJSON function :
if(related.get( key ) instanceof Backbone.Model && _.isFunction(related.get( key ).toJSON)){
value[ key ] = related.get( key ).toJSON(options);
var nValue = {};
var subInJSON = related.getRelation(key).options.includeInJSON;
for(var nkey in subInJSON){
nValue[subInJSON[nkey]] = value[key][subInJSON[nkey]];
}
value[key] = nValue;
}else{
value[ key ] = related.get( key );
}
Sorry for my English