Answer the question
In order to leave comments, you need to log in
backbone. Collection inside model?
Hello. I'm trying to implement a simple structure like this:
Model 1
Submodel1
Submodel2
Submodel3
Model2
Submodel1
Submodel2
etc.
I did not find examples with the implementation of such a task. The set of submodels must be a collection for each main model, which in turn is part of the main collection, of course.
Faced similar? If so, please help with the implementation or give a small example. Thanks
Answer the question
In order to leave comments, you need to log in
A slightly more sophisticated version .
And this should be the best fit.
Making a collection part of another collection by standard methods will not work. Why not just add the model to the shared collection and to the collection in the model?
var Model = Backbone.Model.extend({
collection: null,
initialize: function(){
this.collection = new Collection();
}
});
var Collection = Backbone.Collection.extend({
model: Model
});
var collection = new Collection([{}]),
submodel = new Model({});
collection.add(submodel);
collection.at(0).collection.add(submodel);
console.log(collection.at(1) === collection.at(0).collection.at(0)); // true
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question