1

我在 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没有正确设置属性吗?

4

1 回答 1

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;    
            }
        });
于 2012-09-07T14:39:01.857 回答