Y
Y
Yuri Musienko2014-01-27 15:36:40
JavaScript
Yuri Musienko, 2014-01-27 15:36:40

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

2 answer(s)
A
aen, 2014-01-27
@musienkojs

A slightly more sophisticated version .
And this should be the best fit.

V
Vasily Ostanin, 2014-01-27
@bazilio91

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

jsfiddle.net/bazilio91/rpuT8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question