Pravin Mane
Pravin Mane

Reputation: 529

In extjs4 how to call function inside another function?

I am working in Extjs4 and I am getting stuck at a point where I want to call a function inside another function.

Here I am going to call hideLoginWindow() function in success function.

How can I call hideLoginWindow() function inside success function. Here Is my controller file code

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

When I am running this code I get error which is

Uncaught TypeError: Object [object Object] has no method 'hideLoginWindow'

How can I call hideLoginWindow() function inside success function.

Upvotes: 1

Views: 232

Answers (2)

JamesHalsall
JamesHalsall

Reputation: 13475

You have to ask yourself, what is this at the point of your method call? Because that call is inside another function, the scope is now inside that function and not in the outer object where your hideLoginWindow() method resides.

The best thing to do is to set the scope of your check.save method before you call it...

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

This will retain the scope of the outer object when you enter your function call

Upvotes: 1

Evan Trimboli
Evan Trimboli

Reputation: 30082

You need to set the scope of the save callbacks:

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

Upvotes: 1

Related Questions