Ext.util.Observable.observe中的ExtJS调用控制器方法
  我有我的4.2.1 ExtJS应用程序监听每个Ajax请求( Ext.data.Connection ),以便我可以自定义错误处理和其他东西。 
  我在我的Main Controller内执行此操作 
Ext.define('App.controller.Main', {
        extend: 'Ext.app.Controller',
        init: function (application) {
            var me = this;
            this.control({
                '[xtype=login] button#btnLogin': {
                    click: me.onLogin
                },
            });
            Ext.util.Observable.observe(Ext.data.Connection, {
                requestcomplete: function (conn, response, options) {
                    // Do stuff on success
                },
                requestexception: me.handleRequestException // Do stuff on failure
            });
        },
        handleRequestException: function (conn, response, options) {
            var me = this;
            if (response.status == 401) {
                // here i need to fire the logoutApplication method
                // have tried: me.fireEvent('logoutApplication');  but fireEvent is undefined because "me" scope is data.Connection
                // it doesnt know about the controller.
            }
        },
        logoutApplication: function () {
            var me = this;
            // do somestuff here!!
        }
  在handleRequestException函数内部我试图做me.fireEvent(...)但是fireEvent是undefined因为函数的scope是data.Connection 。 
我不想使用:
var mainController = App.app.getController('App.controller.Main');
  因为当调用init被触发时,我遇到两次事件触发的问题。 
  Im在ExtJS是新的,所以我确定有一个更好的方法来做到这一点。  欣赏任何建议。 
  您可以将scope添加到侦听器配置: 
Ext.util.Observable.observe(Ext.data.Connection, {
     requestcomplete: function (conn, response, options) {
         // Do stuff on success
     },
     requestexception: me.handleRequestException, // Do stuff on failure
     scope: me // add the scope here
});
现在,您的侦听器将使用该范围而不是调用者范围执行。
链接地址: http://www.djcxy.com/p/19549.html上一篇: ExtJS call controller method inside Ext.util.Observable.observe
