Backbone-relational icon indicating copy to clipboard operation
Backbone-relational copied to clipboard

toJSON with sub relations models

Open anthonybriand opened this issue 10 years ago • 0 comments

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

anthonybriand avatar Jun 05 '14 10:06 anthonybriand