Simple starting point for Ember.js fiddles
HTML
<script src="https://github.com/downloads/emberjs/ember.js/ember-0.9.5.js"></script>
<script type="text/x-handlebars" >
</script>
JavaScript
App = Ember.Application.create({});
App.Parent = Ember.Object.extend({
foo: 'someValue',
child: null,
setParentOnChild: function() {
var child = this.get('child');
if (child) {
child.set('parent', this);
}
}.observes('child'),
init: function() {
this.setParentOnChild();
}
});
App.Child = Ember.Object.extend({
init: function() {
this._super();
// you can't access your parent here
// but you can subsequent to assigning
// the object to a parent instance
},
log: function() {
var p = this.get('parent')
console.log("I am " + this.toString() + ". My parent's 'foo' is '" + p.get('foo') + "'");
}
});
console.log("Creating a parent with a child.");
p = App.Parent.create({
foo: 'some other value',
child: App.Child.create()
});
p.get('child').log();
console.log("Setting a new child on the parent.");
p.set('child', App.Child.create());
p.get('child').log();