Skip to content

Instantly share code, notes, and snippets.

Created June 12, 2013 20:01
Show Gist options
  • Save anonymous/5768596 to your computer and use it in GitHub Desktop.
Save anonymous/5768596 to your computer and use it in GitHub Desktop.
// JSON tree is this kind:
[
{
name: "Ob1",
subobjects: [
{name: "Un1", age: 2},
{name: "Un2", age: 5},
{name: "Un3", age: 4},
{name: "Un4", age: 5},
]
},
{
name: "Ob2",
subobjects: [
{name: "Un5", age: 2},
{name: "Un6", age: 5},
{name: "Un7", age: 4},
{name: "Un8", age: 5},
]
}
]
// And I have models and collections like this:
var SubObject = Bacbone.Model.extend({});
var SubObjectCollection = Backbone.Collection.extend({
model: SubObject,
url: "/api/mainobjects/"
});
var MainObject = Bacbone.Model.extend({
initialize: function(){
this.subobjects = new SubObjectCollection(this.get('subobjects'));
}
});
// I have views like this:
SubObjectView = Backbone.Marionette.ItemView.extend({
template: "#subobject-template"
});
MainObjectView = Backbone.Marionette.CompositeView.extend({
initialize: function(){
// This is where everything fails
this.collection = this.model.subobjects;
},
itemView: SubObjectView,
appendHtml: function(cview, iview){
cview.$('ul').append(iview.el);
}
});
MainObjectCollectionView = Backbone.Marionette.CompositeView.extend({
template: "#mainobject-template",
itemView: MainObjectView
});
// The problem happens when MainObject is not yet fetched and so this.model is unknown
// Question is, how I can make fetched models subobjects as collection of CompositeView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment