Call controller's function from another contro

2019-01-24 06:52发布

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

3条回答
The star\"
2楼-- · 2019-01-24 07:21

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).

查看更多
你好瞎i
3楼-- · 2019-01-24 07:24

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();
查看更多
Viruses.
4楼-- · 2019-01-24 07:27

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();

查看更多
登录 后发表回答