3 ms·
Where can I read more about changes related to nested model parsing and submodels? This is something I had to implement just a week ago, and I resorted to overr
by danabramov 13y ago
Where can I read more about changes related to nested model parsing and submodels? This is something I had to implement just a week ago, and I resorted to overriding `set`. I wonder if they made it easier, or if there is a builtin support for nested models of some kind.
- jashkenas 13y agoHappy to oblige. There's no built-in convention for it, but you can do something like this: parse: function(attrs) { this.friends = new PeopleCollection(attrs.friends); delete attrs.friends; return attrs; } ... assuming that friends is an array of JSON objects suitable to be transformed into Person models.
- danabramov 13y agoJeremy, thanks! I tried overriding Model's `parse` in 1.0 but wasn't satisfied because it seems to be called in some cases but not the others. (I can't recall a specific example right now; maybe it was parsing the collection from server, or creating a collection using constructor). Do you think there are drawbacks to overriding `set`? It seems to work well for me but I'd love to hear your opinion: https://gist.github.com/gaearon/6689379 https://gist.github.com/gaearon/6689379
- philfreo 13y agoIf you want something lightweight I'd go with the approach @jashkenas mentioned. But also might want to checkout: https://github.com/PaulUithol/Backbone-relational https://github.com/PaulUithol/Backbone-relational