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 |
Not entirely sure I understand... are you trying to lazy-load the subFoos when you need them?
FooModel = Backbone.Model.extend({
getSubFoos: function(){
if (!this.subFoos){
this.subFoos = new FooCollection({parentId: this.id});
subFoos.fetch();
}
return this.subFoos;
};
});
FooCollection = Backbone.Collection.extend({
initialize: function(data, options){
this.parentId = options.parentId;
}
url: function(){
var url;
if (this.parentId){
url = "/foo/" + this.parentId + "/SubFoos";
} else {
url = "/SomethingElse/WithNo/ParentId";
}
return url;
}
});
Something like that should work, I think.
yes on lazy-loading the sub foos.
I'm thinking more something like
class SubFoos extends Backbone.Collection
url: "http://website.com/whatever"
initialize: ->
@url = "http://website.com/foos/#{@parent_id}/subfoos"
class BigPapaFoo extends Backbone.Model
@getSubFoos: ->
@subFoos = new SubFoos(parent_id: 12345)
@subFoos.fetch()
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:
@model.getSubFoos()
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
that's really compatible with a REST API which sends you a Foo and its sub-Foos, but the thing I'm doing right now, I basically need to go "hey Rails app, I have this Foo, give me its sub-Foos. here's the id for the parent Foo."
so it's really not so far away from a bog-standard Backbone Collection, but it's a Collection which has a concept of a single id for itself, which is not sane in the pure Backbone world.
I could use a setup like what you have here to do things just a little differently -- I have the id, so I reload the individual Foo, and this time the server sends along the associated sub-Foos as well. but it seems a little circuitous. it seems like it might be easier if I just give the sub-Foos Collection a new attribute representing the id of its parent Foo, and also give it a method whereby it modifies its own url attribute, to reconstruct it with that parent Foo id, upon being initialized.
does that make sense? it seems a lot less complicated than backbone-relational.