1

I feel like I must be missing something obvious, but how does one access the parent object from within a child object defined by hasMany in the parent?

We have a hierarchical data model.

  • App has many Categories
  • Category has many Layers
  • All of our relationships are embedded.

We are side loading the whole shebang from a big old JSON object like:

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"
        }]
    }]
}

and the models look (roughly) like:

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

OK, so then we:

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

So given all that, from within a method of a App.Layer instance, how to I do something like this.get('parent') to get a handle on the App.Category object to which the layer belongs?

pjfingers
  • 51
  • 5

0 Answers0