How can I access methods of another controller (or should I use services instead?)


#1

For example: when a user creates an account in the index route to be able to automatically run a method (e.g. createSomething) from the home route.

I tried the controllerFor but couldn’t get it to work from another route, as the method didn’t return any controllers.

Technically this is a method I need available in different parts of my application, which implies a service. But it seems wasteful to create a service just for something so simple.

Is there another approach?


#2

It seems perfectly reasonable to use a service here. Most of my services are pretty small.


#3

UPDATE: two other approaches are Ember.extend() or Ember.Mixin.create(), as discussed in this article.

If I need a method to persist throughout my application, I’d prob go for services; but if I only need to use certain properties/methods in one or two controllers, then extends or mixins are more appropriate (or could even use them together to create helper properties for a service).


#4

Agreed! Because you specifically mentioned that you wanted to run another controller’s method and not a method, I assumed that there was a dependency on the other controller’s state.

If you’re just looking to execute the same logic in different contexts, then inheritance or mixins make more sense. You could even create a utility method and import it where you want to use it. While services could be used as stateless utilities, the real advantage to services is that they are widely available singletons.


#5

Note: you can also inject controllers, i.e., Ember.inject.controller('someController'), to get their properties and actions.