Ember Objects (and parent objects) best practices


#1

I am building an app with a pretty basic object hierarchy, and it is often the case I have objects that are included in my own container objects and need a reference to their parent container.

I have illustrated below how I am currently setting up a reference to the parent container on object creation, but I am wondering if there are pitfalls with this approach and/or better ways to accomplish the same thing. This doesn’t seem like a great way to do it. Thoughts?


// Some basic content object class
var myObject = Ember.Object.extend({
    someProperty:  null,
    anotherProperty: null,
    
    //reference to container
    container:  '?'
});

// Some basic container class
var containerClass = Ember.Object.extend({
   prop1:  'someVal',
   prop2:  'anotherVal',

   arrObjects: null,

   init:  function() {

      var array = new Array();

      //create an array of |myObject|s
      for(var i = 0; i < 10; i++){
           var obj = myObject.create();
           
           //Explicitly set container reference here?
          obj.set('container', this);
         array.pushObject(obj)
      }
      
     this.set('arrObjects', array);
  }
});

#2

Pitfalls? Can’t think of any without knowing more detail about the purpose of all this. You can make it look a bit prettier by doing

array.pushObject(myObject.create({
    container: this
});    

but that’s not fundamentally any different.


#3

I was thinking about pitfalls in the sense of creating circular references that would make it so neither object would ever get taken out of memory. In other languages, I would set up a weak reference to the parent in a situation like this.

This works for now though. Thanka!