ApplicationRoute for actions used by multiple controllers


#1

I was wondering why actions don’t go to the Application Controller if they don’t exist in the current controller. Instead they go to the ancestor routes as described here: http://emberjs.com/guides/templates/actions/.

My use case is: I have an action (add item to shopping cart) that is referenced in multiple templates so the action is currently in the application route. There is a property (active shopping cart) on the application controller that is referenced in other controllers using Ember.computed.alias. It needs to be updated by the add to cart action in the application route as well but it doesn’t look like you can do Ember.computed.alias(‘controller.application.activeCart’) in routes. I am displaying the number of items in the cart in the application template so I have a property on the application controller that needs to update when items are added/removed from the cart.

Just curious what the best practice for this type of case would be.


#2

How about using this.controllerFor('application').set('activeCart', ...);? Wouldn’t that work for you?


#3

I would create a separate controller for the shopping cart. That way you can needs it from other controllers but yet keep it separate from the application controller. For the actions, put the actual logic in the shopping cart controller but call .send() from the other controllers. The upcoming Ember Services will probably replace this in Ember 2.0 however. Here is an example, although not perfect, you get the idea.