0
votes

I thought backbone-relational automatically parses and makes nested models ready of nested json. I have a big json like this

{  //ItemResultModel
   "items":[  
      {  //ItemModel
         "id":120514,
         "recordDate":"2013-10-19T00:00:00",
         "owner":{  
            "id":"d14a052b-a9df-45ba-92e5-58adfe28c10c",
            "firstName":null,
            "lastName":null
         },
         "features":[  
            {  //FeatureModel
               "id":1,
               "properties":[  
                  {  //PropertyModel
                     "id":814518,
                     "values":[  
                        "5"
                     ]
                  }
                  //,other properties
               ]
            }
           //, other features
         ]
      }
   ],

   //other models and collections
   "facets":{  

   },
   "totalCount":7
}

And i tried to parse this json to my models by using backbone relational. And here are my models:

app.Models.ItemResultModel = Backbone.RelationalModel.extend({
    relations: [{
        type: Backbone.HasMany,
        key: 'items',
        relatedModel: 'app.Models.ItemModel',
        collectionType: 'app.Collections.ItemCollection',
        parse: true   
    }]
});

  app.Collections.ItemCollection = Backbone.Collection.extend({
        model: app.Models.ItemModel
    });

app.Models.ItemModel = Backbone.RelationalModel.extend({
    urlRoot: '/api/Item',
    relations: [{
        type: Backbone.HasMany,
        key: 'features',
        relatedModel: 'app.Models.FeatureModel',
        collectionType: 'app.Collections.FeatureCollection',
        parse: true    
    }]       
});
app.Models.FeatureModel = Backbone.RelationalModel.extend({
    relations: [{
        type: Backbone.HasMany,
        key: 'properties',
        relatedModel: 'app.Models.ItemPropertyModel',
        collectionType: 'app.Collections.ItemPropertyCollection'
    }]
});

app.Collections.FeatureCollection = Backbone.Collection.extend({
    model:app.Models.FeatureModel
});

and the same schema goes on for properties and deeper levels. The problem is, when i fetch the ItemResultModel from server i don't get my models populated after second level, i mean, i don't get features models.

this.model.get('items'); // this has some item models as i expect
this.model.get('items').at(0).get('features');//items does not have any feature model

How can i make my models ready after fetching the wrapper model? If you offer some solution without using backbone-relational, by using parse method, it is ok too.

2

2 Answers

0
votes

if you use simple backbone model?

this.model.get('items')[0].features

or if you create collection based on items array:

this.collection.at(0).get('features')
0
votes

Now I realized there are similar question. Sorry for this. No need to use backbone-relational for this.

 app.Models.ItemResultModel = Backbone.Model.extend({
        subCollections: {
            items: app.Collections.ItemCollection
        },
        parse: function (response) {
            //Solution
            this.set({
                itemsCollection: new app.Collections.ItemCollection(response.items, { parse: true })
            });
            delete response.items;

            //Better solution
            for (var key in this.subCollections) {
                var embeddedClass = this.subCollections[key];
                var embeddedData = response[key];
                response[key] = new embeddedClass(embeddedData, { parse: true });
            }

            return response;
        }
    })

And you can do the same thing for all levels of hierarchy.