jeromegn/Backbone.localStorage — Issue #16: Nested Collections/Models
Author: basteln
Created: 2011-11-06 17:36:42+00:00
State: open

I'm having problems with this extension and nested collections/models.

Let's say I have a Backbone.Model called "FootballTeam". Inside this model I have a name "FoobarRangers" and a Backbone.Collection "players". The "players" collection contains many instances of "Player", which is a Backbone.Model.

When I do myFootballTeam.save(), the "players" collection is stored as a normal array, and the players in it are stored as normal JavaScript objects. That's good, because it saves space. But when I do: myFootballTeam.fetch(), the "players" collection gets overwritten by the normal JavaScript array, and the players inside it aren't instances of "Player" anymore, they are just JavaScript objects.

For now I have written the "parse" method of FootballTeam to work around this:
```javascript
parse: function(response) {
var modifiedResponse = response;
modifiedResponse.players = new (Backbone.Collection.extend({model: Player}))(response.players);
return modifiedResponse;
}
```

Of course this isn't good because I'd have to do it by hand in many places. Is there a better way?