PDA

View Full Version : never, ever call a controller method from a view



fordprefect
17 Mar 2012, 8:51 PM
Apparently this went from *how* (you can) to *should* (no you shouldn't).

vietits
18 Mar 2012, 12:47 AM
In your case, myapp is just a namespace where all your application reside. You should assign your application object to this namespace before using it globally. This can be done in the launch() method.


Ext.application({
name: 'myapp',
...
launch: function(){
...
myapp.application = this;
}
});

Then you can refer to a controller from any point in your application with this code


myapp.application.getController('Home');

fordprefect
18 Mar 2012, 8:16 PM
Thanks

börn
18 Mar 2012, 11:47 PM
maybe you should consider to not call the controller method from a view, because there was a reason that you can't access it directly ;)

if you want to communicate with your controller use events...

castitas
14 May 2012, 9:27 AM
The docs have good examples of using events in the controller (http://docs.sencha.com/ext-js/4-1/#%21/api/Ext.app.Controller) (see the second code box). It really is easier (and best practice).

fordprefect
14 May 2012, 10:53 AM
Very true. It is easier and it is best practice.

14 May 2012, 5:35 PM
maybe you should consider to not call the controller method from a view, because there was a reason that you can't access it directly ;)

if you want to communicate with your controller use events...

börn is absolutely correct. There seems to be a growing sense of bad practice around where people are wiring up views to have references to their controllers. This is not recommended.

fordprefect
15 May 2012, 6:50 AM
Calling a controller from a view has always been and will always be bad practice...