我在ExtJs中创建了一个类,包括变量和函数,用于修改类的内部属性。
当我想要更改类的内部属性(来自类的内部或外部函数)时,我的问题很突出。
班级:
var Ws = new Ext.Class({
WsUrl: '',
WsErrorCode: 0,
WsOut: null,
CoreVersion: '',
AppName: '',
AppVersion: '',
BrowserLanguage: GetBrowserLanguage(),
BrowserUserAgent: GetBrowserLanguage(),
BrowserToken: '',
constructor: function(url) {
this.WsUrl = url;
return this;
},
SendCmd: function(CmdName) {
var WsOut;
var WsErrorCode;
//Send request
Ext.Ajax.request({
loadMask: true,
url: this.WsUrl,
params: {cmd: CmdName},
success: function(response, opts) {
WsOut = Ext.decode(response.responseText);
WsErrorCode = response.status;
},
failure: function(response, opts) {
WsOut = Ext.decode(response.responseText);
WsErrorCode = response.status;
}
});
this.WsOut = WsOut;
this.WsErrorCode = WsErrorCode;
return this;
}
});
这里创建了对象:
var WsTest = new Ws('ws.php');
WsTest = WsTest.SendCmd('GetBrowserToken');
alert(WsTest.WsErrorCode); //Here, the code must return 200 but the value is 0 ... why ?
您是否知道为什么This.WsOut = WsOut
没有正确设置属性?
答案 0 :(得分:0)
您的问题是该请求是异常的。因此,当您分配wsOut时,请求仍在进行中。 将范围添加到请求并在回调函数上设置内部属性。
Ext.Ajax.request({
loadMask: true,
url: this.WsUrl,
scope:this,
params: {cmd: CmdName},
success: function(response, opts) {
this.WsOut = Ext.decode(response.responseText);
WsErrorCode = response.status;
},
failure: function(response, opts) {
this.WsOut = Ext.decode(response.responseText);
WsErrorCode = response.status;
}
});