Note: I'm a total ignoramus regarding javascript.
I've broken my ExtJS 4.1 MVC app out into several controllers like:
/app/controller/Auth
| |Quiz
| |Result
| |Blah...
|model/...
I want to respond to an "event", not a DOM Event, rather a Ext.form.action.Submit.success
event by calling functions in both my Auth
and Quiz
controllers. The summarized code for the first part is here:
// File: app/controller/Auth.js
attemptLogin : function() {
var form = Ext.ComponentQuery.query('#loginpanel')[0].form;
if (form.isValid()) {
form.submit({
success : function(form, action) {
// THIS IS THE FUNCTION FROM THE CURRENT CONTROLLER
Assessor.controller.Auth.prototype.finishLogin();
// THIS IS THE FUNCTION FROM THE OTHER CONTROLLER
Assessor.controller.Quiz.prototype.setupAssessment();
},
This works but feels wrong. Is there a proper way to do this? It seems like I should fire a unique event that is listened to by both controllers, but I can't understand how to do that with Ext.Event
. Any guidance?
Thanks! I'm really grateful for all the great ideas and advice.
I have also been looking for this and all you need is Asanda.app.getController('quiz').setupAssignment();, where Asanda is the name of your app
It makes sense to me to fire a custom event from the form and simply listen to it in both your controllers, like what you said here:
Then in each of your controllers you can listen to it with Controller#control, like this:
And then add the same thing to your
Quiz
controller:I've used this approach successfully in 4.1.0 and 4.1.1
It really should be
or something along these lines (in order to have a correct
this
reference that points to the 'owner' of the method, the controller object)However, why do you use this unorthodox way to call the current controller's method. Just set the
scope
for the success callback, then call this.finishLogin().Also, you can retrieve another controller instance using
Controller#getController
.Then, if your controller methods are not depending on each other, you could make them both listen to the same event.
Another solution is to fire a custom event once the login is finished. You could do that on the application object
and in your controller's init method:
Please note that you cannot listen to those events via
Controller#control
- see Alexander Tokarev's blog post for a patch to Ext to achieve this.There is no standard way to fire events between controllers, but it's possible with some custom hacks. See my recent blog post.
You should use a MessageBus if you have to send events between controllers:
store the message bus in a global var
Where you have to send events:
Where you have to receive events: