Communicating with Parent Controller


#1

What’s the recommended way of sending messages from a child controller to a parent controller? I’m currently doing the following:

this.get('parentController').someMethod(param);

This works, but it is fragile as the child controller has knowledge of the parent’s implementation and the parent must implement this method.

I’d like to use Ember.Evented as this would remove the need for the parent to implement a method, it could subscribe to the child events if it wanted to. It seems as though I would need to add a subscription every time a child controller is added though:

childController.on('someEvent', function() {
 ...
});

Is there a hook provided for every time a new child controller is added? If so, I could use that.

In an ideal world, I would like Ember to provide a facility to bubble events from child controllers up through the hierarchy of descendant controllers, something like:

controller.publish('someEvent', arg1, arg2);

My use case for this is one where I have an ArrayController which contains a number of child ObjectControllers. The ObjectControllers provide a delete method and I’d like to ask the ArrayController to delete our model.


#2

By default the send method will bubble the actions from the controller to its targets, and since the target of the childController will be the arrayController, send should satisfy your requirement. Take a look at this sample JSBin


#3

That works a treat, thanks.


#4

I feel it’s worth replying after spending an hour this morning figuring out why I had to name the parent controller’s function name different from the childs. (ie: ‘alertthis’, ‘alertthi’)

I had a discussion in #emberjs on freenode and this was the solution @mixonic posted to prevent recursive calls to the child which will end up crashing your browser.