Created
September 5, 2012 19:58
-
-
Save mxriverlynn/3643596 to your computer and use it in GitHub Desktop.
a very basic nested model structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = { | |
name: "foo", | |
subFoos: [ | |
{name: "bar"}, | |
{name: "baz"}, | |
] | |
}; | |
FooModel = Backbone.Model.extend({ | |
initialize: function(){ | |
this.setupSubFoos(); | |
}, | |
setupSubFoos: function(){ | |
var subFoos = this.get("subFoos"); | |
this.unset("subFoos", {silent: true}); | |
this.subFoos = new FooCollection(subFoos); | |
} | |
}); | |
FooCollection: Backbone.Model.extend({ | |
model: FooModel | |
}); | |
// i extracted the "setupSubFoos" in to a function called `getChildren` so that i could do something like: | |
this.subFoos = this.getChildren("subFoos"); | |
// and then i could put that one liner in the initializer of the model, directly |
yeah, that looks a bit nicer in implementation
sweet! much thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes on lazy-loading the sub foos.
I'm thinking more something like
and then in the view, when you drill down from viewing a collection of foos to a detail view where you see just one foo and its associated subfoos: