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/...
我想通过调用 中的函数来响应事件",而不是 DOM 事件,而是响应 Ext.form.action.Submit.success
事件>both 我的 Auth
和 Quiz
控制器.第一部分的总结代码在这里:
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#control 收听它,如下所示:
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);
}
}
然后将相同的内容添加到您的 Quiz
控制器:
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屋!