10

For the moment, when I'm in a controller and that I want to call a function from another controller, I do this :

App.app.getControllerInstances()['App.controller.OtherController'].do_something();

Is seems a bit heavy to me, is there another (better) way to do this ?

Thanks

Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121

3 Answers3

30

I would use the getController method: http://docs.sencha.com/touch/2-0/#!/api/Ext.app.Application-method-getController

EG: this.getApplication().getController('ControllerName').doSomething();

badsyntax
  • 9,394
  • 3
  • 49
  • 67
11

If you're not in the context of your Controller(for example in a callback function of some object), you can do this.

MyAppName.app.getController('ControllerName').doSomething();
Niklas
  • 23,674
  • 33
  • 131
  • 170
7

When using the MVC convention in Sencha Touch 2, I would recommend the following when trying to call a method called 'SomeMethodInB' in 'ControllerB' from inside 'ControllerA':

MyAppName.app.getController('ControllerB').

'MyAppName' is the name of the app you defined in the core app definition - typically in your app.js file.

According to the Sencha forums, the below is depreciated:

this.getApplication().getController('ControllerB').SomeMethodInB();

http://www.sencha.com/forum/showthread.php?158996

In fact the only way I can call the "this.getApplication()" method to even work is when calling it from my app definition file (app.js).

Don Dimon
  • 71
  • 1
  • 2