在extjs4中如何调用另一个函数内的函数?

时间:2013-03-02 11:07:48

标签: javascript extjs extjs4

我在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

当我运行此代码时,我收到错误

未捕获TypeError:对象[object Object]没有方法'hideLoginWindow'

如何在成功函数中调用hideLoginWindow()函数。

2 个答案:

答案 0 :(得分:1)

您需要设置保存回调的范围:

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

答案 1 :(得分:1)

你必须问自己,方法调用时this是什么?因为该调用位于另一个函数内部,所以范围现在位于该函数内部,而不是在hideLoginWindow()方法所在的外部对象中。

最好的办法是在调用之前设置check.save方法的范围......

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

当您输入函数调用时,这将保留外部对象的范围

相关问题