从hasMany的子级访问父级

时间:2014-06-19 13:54:05

标签: ember-model

我觉得我必须遗漏一些明显的东西,但是如何从父母中的hasMany定义的子对象中访问父对象?

我们有分层数据模型。

  • App有很多类别
  • 类别有多个图层
  • 我们所有的关系都是嵌入式的。

我们正在从一个大的旧JSON对象中加载整个shebang,如:

var json_data = {
    "app_id": 100,
    "name": "My Cool App"
    "categories": [{
        "cat_id": 100,
        "name": "First Category",
        "layers": [{
            "layer_id": 100
            "name": "First Layer"
        },{
            "layer_id": 200
            "name": "Second Layer"
        }]
    },{  
        "cat_id": 200,
        "name": "Second Category",
        "layers": [{
            "layer_id": 300
            "name": "Third Layer"
        },{
            "layer_id": 400
            "name": "Fourth Layer"
        }]
    }]
}

并且模型看起来(大致)像:

App.AppConfig = Ember.Model.extend({
  app_id: attr(),
  appName: attr(),
  categories: hasMany('App.Category', {key: 'categories', embedded: true})
});


App.Category = Ember.Model.extend({
  cat_id: attr(),
  name: attr(),
  layers: hasMany('App.Layer', {key: 'layers', embedded: true})
});


App.Layer = Ember.Model.extend({
  layer_id: attr(),
  name: attr(),

  init: function() {
    this._super();
    this.on('didLoad', this, this.setDefaults);
  },

  setDefaults: function() {
    //This is the line I can't figure out
    var my_parent_category = this.get('parent');
  }  
});

好的,那么我们:

var appConf = App.AppConfig.create();
appConf.load(1, json_data);

所以考虑到这一点,从App.Layer实例的方法中,我如何做类似的事情 this.get('parent')获取图层所属的App.Category对象的句柄?

0 个答案:

没有答案