Architecturally speaking, a controller does not know about view (usually) and it certainly doesn't know about some other controllers view. Controllers don't know about other controllers either. Sometimes it is necessary for these loosely coupled components to be able to communicate, but best practice is not to tightly couple them together. Is there a messaging framework plugin that allows different components to emit and receive global messages within the app? Is there a better way to handle these kinds of communications?
-
Perhaps this question could help you: http://stackoverflow.com/questions/12799293/ember-js-connectcontrollers#comment17329873_12799293 and, http://emberjs.com/api/classes/Ember.ControllerMixin.html#method_connectControllers – sly7_7 Oct 10 '12 at 19:03
-
@sly7_7 Thanks! That helps, it feels kinda hackish, but it would work. I just hate the strong coupling; it doesn't feel right having to make them aware of each other. – wmarbut Oct 10 '12 at 19:12
-
Yes, but for now, I don't know how to do it without the connectControllers :( – sly7_7 Oct 10 '12 at 19:15
2 Answers
It wouldn't be too hard to create a pub/sub hub using something like jQuery.Callbacks. However, that is not part of a typical Ember.js app.
When something happens that needs to be communicated beyond the scope of the responsible controller, the pattern is to send the action up to the router, where the current state can handle it. The router/stateManager is the coordinator of the app, and can respond to the action by sending messages to other controllers and/or models, and/or by transitioning to another state.
I tried to diagram this in a talk I gave recently: http://www.lukemelia.com/devblog/archives/2012/08/23/architecting-ember-js-apps/

- 8,389
- 33
- 41
-
Thanks Luke :). In this presentation, you mentioned connectControllers too, so I was thinking it was a good way for sharing data between controllers. That's why I proposed this as a comment. Were I wrong ? – sly7_7 Oct 11 '12 at 09:44
-
@sly7_7 when one controller is presenting data from another, then connectControllers is great. However, wmarbut was asking a more general question, about how different components communicate in Ember apps. – Luke Melia Oct 11 '12 at 18:18
-
Humm, as always, great explanation. Thanks again :) I think I have to tell 2 words @tomdale, he wanted so subjects to document. I think examples of using either connectControllers, or sending to router which forwards to other controllers should be a good thing. – sly7_7 Oct 11 '12 at 21:29
I'm still new to Ember, but wouldn't Ember Instrumentation be useful in this case? It allows to publish and subscribe to custom events. Great explanation can be seen in this answer.
-
1I think both are valid. Pretty sure Ember Instrumentation wasn't around when @Luke posted his answer – Mike Grassotti Mar 25 '13 at 03:23