0
votes

I have 3 models on backbone:

var Level1Model = Backbone.Model.extend({
    defaults: {
        level2Collection: null
    }
});

var Level2Model = Backbone.Model.extend({
    defaults: {
        level3Collection: null,
        text: null
    }
});

var Level3Model = Backbone.Model.extend({
    defaults: {
        text: null
    }
});

I have two REST services (urls):
1. One that gets Level1Model id and returns Level1Model with Level2Model and id's of Level3.
For example:

{
    Level2Collection: [
        { 
            text: "aaa",
            Level3Collection: [ {id:1}, {id:2}, {id:3} ]
        },
        {
            text: "bbb",
            Level3Collection: [ {id:4}, {id:5} ]
        }
    ]
}

2. One that gets Level3Model id and returns Level3Model data.

I am looking a way to fetch all the data structure by doing:

var level1Ins = new Level1Model({id:123});
level1Ins.fetch({
    success: function() {
        doSomething();
    }
});

I am really confused of how to do it. For example, I don't know how can I fill the Level3Collection and also call doSomething() when success loading all elements.
How can I load the entire level1 instance?

1
So, just to make sure I understand ... your level1Ins.fetch() in the example would have to make 6 REST calls: one to get the Level1 model, and 5 to get data for each Level3 model in each Level2 model? - McGarnagle

1 Answers

0
votes

You should try Backbone Relational. It makes this kind of thing very easy to work with.