骨干关系解析嵌套模型

时间:2014-10-26 11:41:06

标签: backbone.js backbone-relational

我认为骨干关系会自动解析并使嵌套模型准备好嵌套的json。我有一个像这样的大json

{  //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
}

我试图通过使用骨干关系解析这个json到我的模型。这是我的模特:

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
});

和属性和更深层次的相同模式。问题是,当我从服务器获取ItemResultModel时,我没有得到我的模型在第二级后填充,我的意思是,我没有得到功能模型。

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

如何在获取包装模型后使模型准备好?如果你提供一些不使用骨干关系的解决方案,通过使用parse方法,也可以。

2 个答案:

答案 0 :(得分:0)

如果你使用简单的骨干模型?

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

或者如果您根据items数组创建集合:

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

答案 1 :(得分:0)

现在我意识到有类似的问题。非常遗憾。 不需要为此使用骨干关系。

 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;
        }
    })

你可以为所有级别的层次结构做同样的事情。