1

Extjs4で作業していますが、別の関数内で関数を呼び出したいときに行き詰まります。

ここでは、success関数でhideLoginWindow()関数を呼び出します。

成功関数内でhideLoginWindow()関数を呼び出すにはどうすればよいですか。これが私のコントローラーファイルコードです

Ext.define('Am.controller.sn.UserController',
        {

            extend:'Ext.app.Controller',
            stores:['sn.UserStore','sn.SecurityquestionStore'],
            models:['sn.UserModel','sn.SecurityquestionModel'],
            views:['sn.user.Login','sn.user.Registration','sn.user.ForgetMyKey','sn.user.SecurityQuestion','sn.user.KpLogin'],


            init:function()
            {
                console.log('Initialized Users! This happens before the Application launch function is called'); 
                this.control(
                {
                    'Login button[action=loginAction]':
                    {
                        click:this.authenticateUser
                    },
                    'Login button[action=registerAction]':
                    {
                        click:this.registerUser
                    },
                    'KpLogin button[action=loginAction]':
                    {
                        click:this.authenticateUser 
                    }   
                });//end of this.control    
            },//end of init function    
    authenticateUser:function(button)
    {
        ***// this function is called
        this.temp();***

        var email=this.getUserName().getValue();
        var password=this.getPassword().getValue();
        console.log("Email"+email);
        console.log("Password"+password);
        var check = Ext.ModelManager.create(
        {
            primaryEmail:email,
            password: password,
        }, 'Am.model.sn.UserModel');
        check.save(
        {   
            success: function(record, operation) 
            {

                if(operation.request.scope.reader.jsonData["id"]==1)
                {
                    // Code for geting component
                    alert("you are logged in successfully");

                    **//this function is not called
                    this.hideLoginWindow(); // I tried also hideLoginWindow();**

                }//end of if statement


            },//End of success function
            failure: function(record, operation) 
            {
                console.log("Inside failure function");

            },//End of failure function

        });// End of check save function
        console.log("outside authenticated function");

    },//end of authenticate user function


    //***************************Reusable functions********************
    // this function get called
    temp:function()
    {
        console.log("Temp function called");
    },
    //this function is not get called
    hideLoginWindow:function()
    {
        var obj=Ext.ComponentQuery.query('#loginId');
        console.log("Object name = "+obj[0].id);
        obj[0].hide();
    }
});// End of login controller

このコードを実行すると、エラーが発生します。

Uncaught TypeError:Object[objectObject]にはメソッド'hideLoginWindow'がありません

成功関数内でhideLoginWindow()関数を呼び出すにはどうすればよいですか。

4

2 に答える 2

1

保存コールバックのスコープを設定する必要があります。

check.save({
    scope: this,
    success: function() {}
});
于 2013-03-02T11:11:42.270 に答える
1

thisメソッド呼び出しの時点で何が起こっているのか、自問する必要があります。その呼び出しは別の関数内にあるため、スコープはその関数内にあり、hideLoginWindow()メソッドが存在する外部オブジェクトにはありません。

最善の方法は、check.saveメソッドを呼び出す前にメソッドのスコープを設定することです...

check.save({
    scope: this,
    //...
});

これにより、関数呼び出しを入力したときに外部オブジェクトのスコープが保持されます

于 2013-03-02T11:13:09.130 に答える