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 に答える