ExtJS call controller method inside Ext.util.Observable.observe
I have my 4.2.1 ExtJS application listen to every Ajax request ( Ext.data.Connection ) so I can do custom error handling and other stuff.
Im doing that inside my 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!!
}
Inside the handleRequestException function Im trying to do me.fireEvent(...) but fireEvent is undefined because the scope of the function is data.Connection .
I don't want to use:
var mainController = App.app.getController('App.controller.Main');
Because when calling that the init is fired and I get issues with event firing twice.
Im new in ExtJS so Im sure there is a better way to do this. Appreciate any advice.
You can add a scope to the listeners configuration:
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
});
Now you listeners get executed with that scope instead of the caller scope.
链接地址: http://www.djcxy.com/p/19550.html