Nested Collections/Models #16

Closed
ghost opened this Issue Nov 6, 2011 · 2 comments

Projects

None yet

3 participants

@ghost

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:

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?

@karlwestin

Hi there!

I found your post because i was having a similar problem. In the end i came up with a solution that i think was pretty good, without any additional parse function. I wrote a blog post about it here

So, if I understood this problem right, Backbone.LocalStorage already supports this, and this issue can be closed.

Cheers :)

@jeromegn jeromegn closed this Jan 14, 2013
@jeff-h

For anyone who's interested, the post mentioned above is no longer available. You can still see it here:

https://web.archive.org/web/20130327070208/http://karlwestin.posterous.com/nested-modelscollections-and-storage-with-bac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment