ExtJS 4.1从另一个控制器调用一个控制器 [英] ExtJS 4.1 Call One Controller From Another
问题描述
注意:我是关于javascript的完全无知。
我已经把我的ExtJS 4.1 MVC应用程序分成几个控制器:
I've broken my ExtJS 4.1 MVC app out into several controllers like:
/app/controller/Auth
| |Quiz
| |Result
| |Blah...
|model/...
event而不是 DOM Event ,而是通过在两者中调用函数的 Ext.form.action.Submit.success
/ em>我的 Auth
和测验
控制器。第一部分的摘要代码在这里:
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();
},
这种方法可行,但是感觉有问题。发出一个由两个控制器听到的独特事件,但我不能理解如何用 Ext.Event
这样做。任何指导?
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.
推荐答案
我有意义的是,从表单中触发一个自定义事件,只需在你的控制器中听到它,就像你在这里说的:
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:
看来我应该触发一个由
控制器侦听的独特事件
It seems like I should fire a unique event that is listened to by both controllers
// File: app/controller/Auth.js
attemptLogin : function() {
var form = Ext.ComponentQuery.down('#loginpanel').form;
if (form.isValid()) {
form.submit({
success : function(form, action) {
// fire the event from the form panel
form.owner.fireEvent('loginsuccess', form.owner);
},
然后在每个控制器中 可以使用Controller#控件来监听它,如下所示:
Then in each of your controllers you can listen to it with Controller#control, like this:
Ext.define('YourApp.controller.Auth', {
extend: 'Ext.app.Controller',
init: function() {
var me = this;
me.control({
'#loginpanel': {
loginsuccess: me.someHandler
}
});
},
someHandler: function(form) {
//whatever needs to be done
console.log(form);
}
}
然后将相同的东西添加到您的测验
控制器:
And then add the same thing to your Quiz
controller:
Ext.define('YourApp.controller.Quiz', {
extend: 'Ext.app.Controller',
init: function() {
var me = this;
me.control({
'#loginpanel': {
loginsuccess: me.someOtherHandler
}
});
},
someOtherHandler: function(form) {
//whatever needs to be done
console.log(form);
}
}
我在4.1.0中成功使用了这种方法和4.1.1
I've used this approach successfully in 4.1.0 and 4.1.1
这篇关于ExtJS 4.1从另一个控制器调用一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!