1

我在 Extjs4 中工作,我被困在我想在另一个函数中调用一个函数的地方。

这里我将在成功函数中调用 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

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

未捕获的类型错误:对象 [object Object] 没有方法“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 回答