How can I pass values from one function to another in the following code?

When I run the console.log as follows in the initialize function defined in the code below:

 console.log("Checking for Value_Of_Process_Type in Initialize Function ! :" +Value_Of_Process_Type);
    console.log("Checking for Todays Date in Initialize Function ! :" +TodaysDate)

;

I get undefined for both the variables. However, I see the values of both the variables in the following console.log defined inside the click handler of processEmployeeData function:

console.log("Value_Of_Process_Type and Value_Of_TodaysDate are as follows :"+Value_Of_Process_Type+" and "+TodaysDate);

So basically, my variables are not getting passed from the function processEmployeeData to the initialize function. Is there any other approach I can follow to make that happen or anything wrong that I am doing?

function EmployeePage() {

    var self = this;
    

    
    
   this.getNLPData = function (employee_number_) {

   
        var url = AppManager.getWebServiceURL(self.urlNLPKey);
        if (isEmpty(url)) { alert("Invalid URL in getNLPData()"); return false; }
       
       var ajaxRequest = jQuery.ajax({
          data: {
                employee_id: AppManager.employee.id,
                employee_number: employee_number_,
            },
            dataType: "json",
            method: "GET",
            url: url
        })

       .done(function (data_, textStatus_, jqXHR_) {

             self.processEmployeeData(data_.employee_document_list);

        })
        .fail(function (jqXHR_, textStatus_, errorThrown_) {
            alert("Error in getNLPData(): " + errorThrown_);
            return false;
        });

    };


    
    // Initialize the page
    this.initialize = function () {

        var employee_number = AppManager.selectedData["employee_number"];
        console.log("employee_number By calling page:" +employee_number);
		
        var Value_Of_Process_Type = AppManager.selectedData["my_page.Process_Type"];
        var TodaysDate = AppManager.selectedData["my_page.date_value"];
       
		console.log("Checking for Value_Of_Process_Type in Initialize Function ! :" +Value_Of_Process_Type);  //outputs undefined here
		console.log("Checking for Todays Date in Initialize Function ! :" +TodaysDate);     //outputs undefined here
        
                     
    };


  
  this.processEmployeeData = function (collection_) {


        
        var source =
        {
            localdata: collection_,
                datatype: "array"
            };
     var dataAdapter = new $.jqx.dataAdapter(source, {
                loadComplete: function (data) { },
                loadError: function (xhr, status, error) { }
            });
     $("#nlpDocumentPanel").jqxGrid(
            {
            source: dataAdapter,
            width: '1000',
                height: 150,
                columns: [
                      {
                          text: 'Type', datafield: 'nc_type'
                      },
                      {
                          text: 'Process_Type', datafield: 'processType'
                     
                      {
                          text: 'Date', datafield: 'date_value'
                      }
                      
                  ]
             });

      $("#nlpDocumentPanel").on('rowclick',function(event){

           row = event.args.rowindex;
           datarow = $("#nlpDocumentPanel").jqxGrid('getrowdata', row);
		   var response = JSON.stringify(datarow,null,10);
			//console.log("Datarow JSON Response:"+response);
            
		   	
           var Process_Type = datarow["processType"];
		   AppManager.selectData("my_page.Process_Type", Process_Type);

           var  Value_Of_Process_Type = AppManager.selectedData["my_page.Process_Type"];

           var TodaysDate = datarow["date_value"];

           AppManager.selectData("my_page.date_value", TodaysDate);
           
		   var Value_Of_TodaysDate  = AppManager.selectedData["my_page.TodaysDate"];
       

           console.log("Value_Of_Process_Type and Value_Of_TodaysDate are as follows :"+Value_Of_Process_Type+" and "+TodaysDate);

           //initialize(Value_Of_Process_Type,TodaysDate);  

           console.log("Reached below initialize function inside click handler");

          
          
       });

    

    };

Yes,you can initialize them in the EmployeePage function, so that the functions contained within it can all access those variables.

There’s a good piece on variable scope to be found at Demystifying JavaScript Variable Scope and Hoisting

Assuming the above line is supposed to be uncommented, you should actually get a TypeError because you’d have to call that function on self. :-/ Anyway, you’re passing those values, but this.initialize() isn’t actually taking any arguments.

Thanks for your reply. Yes, this line is commented in my code //initialize(Value_Of_Process_Type,TodaysDate);.

What I believe is happening for the following variables in the .on() method / click handler :

var Value_Of_Process_Type = AppManager.selectedData["my_page.Process_Type"]; var Value_Of_TodaysDate = AppManager.selectedData["my_page.TodaysDate"];

is that my initialize function is defined before the click handler and since initialize function is called before user clicks on a particular row, the above variables values are not getting set. What alternative approach I can use here?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.