Topic
3 replies Latest Post - ‏2012-11-07T17:19:48Z by SystemAdmin
SystemAdmin
SystemAdmin
2327 Posts
ACCEPTED ANSWER

Pinned topic how to define global variable in worklight

‏2012-11-07T08:31:08Z |
Hi ,

I am trying to define global variable in worklight in auth.js but it is not working giving null every time.
Please let me know if there is any other way to degine global variable in worklight.
  • phuongvu_0203
    phuongvu_0203
    39 Posts
    ACCEPTED ANSWER

    Re: how to define global variable in worklight

    ‏2012-11-07T08:53:39Z  in response to SystemAdmin
    Hi vsgehlot7,

    try doing this in auth.js

    
    
    //global variables var sunday; var moon = 5;   function test1 () 
    { ...   
    }   function test2 () 
    { ...   
    }
    


    phuongvu
    • SystemAdmin
      SystemAdmin
      2327 Posts
      ACCEPTED ANSWER

      Re: how to define global variable in worklight

      ‏2012-11-07T09:23:44Z  in response to phuongvu_0203
      
      
      /* * Licensed Materials - Property of IBM * 5725-G92 (C) Copyright IBM Corp. 2006, 2012. All Rights Reserved. * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ 
      /* *  This piece of code was added as a part of upgrading your application *  to a new authentication API introduced in Worklight 5.0.0.3 * * Authenticator object that was used previosly is deprecated. *   New challenge handler APIs are serving as a wrapper for original Authenticator object. *        To learn more about challenge handler APIs please refer to Worklight documentation. * */ var userid = 
      ""; var resu= 
      ""; var SEC_CODE= 
      ""; var EMPLOYEE_CODE= 
      ""; var EMPLOYEE_ID= 
      ""; var FIRST_NAME= 
      ""; var LAST_NAME= 
      ""; var ORG_UNIT= 
      ""; var POS_CODE= 
      ""; var RN_DESCRIPTOR= 
      ""; var RN_EMPLOYEE_USER_ID= 
      ""; 
      // ----------------- Challenge handler start ----------------- var challengeHandler1 = WL.Client.createChallengeHandler(
      "realm1"); var busyInd = 
      
      new WL.BusyIndicator(
      'content', 
      {text : 
      'Please Wait While Loading...'
      });   challengeHandler1.isInitialized = 
      
      false; challengeHandler1.isCustomResponse = function(response) 
      { 
      
      if (typeof Authenticator == 
      "undefined") 
      { 
      
      return 
      
      false; 
      }   
      
      if (!this.isInitialized) 
      { this.isInitialized = 
      
      true; Authenticator.init(); 
      }   var isLoginFormResponse = Authenticator.isLoginFormResponse(response); 
      
      if (isLoginFormResponse) 
      { Authenticator.onBeforeLogin(response, null, challengeHandler1.onSubmitButtonClicked); Authenticator.onShowLogin(); 
      } 
      
      else 
      { Authenticator.onHideLogin(); 
      } 
      
      return isLoginFormResponse; 
      };   challengeHandler1.onSubmitButtonClicked = function(reqURL, authParams) 
      { var options = 
      { headers : 
      {
      }, parameters : (authParams && authParams.parameters) ? authParams.parameters : 
      {
      } 
      };   challengeHandler1.submitLoginForm(reqURL, options, challengeHandler1.submitLoginFormCallback); 
      };   challengeHandler1.submitLoginFormCallback = function(response) 
      { var isLoginFormResponse = challengeHandler1.isCustomResponse(response); 
      
      if (!isLoginFormResponse) 
      { challengeHandler1.submitSuccess(); 
      } 
      }; 
      // ----------------- Challenge handler end -----------------     
      /* Authenticator skeleton; Uncomment and implement if your application requires authentication. */   var Authenticator = function() 
      {   
      
      return 
      {   init : function() 
      { 
      // Authenticator initialization code require( [ 
      "dojo", 
      "dojo/_base/connect" ], function(dojo, connect) 
      { connect .connect( dojo.byId(
      "submitAuthBtn"), 
      "onclick", null, function() 
      { var username = document .getElementById(
      "usernameInputField").value; var password = document .getElementById(
      "passwordInputField").value; 
      //                                                                                      alert(username +":::"+password); busyInd.show(); 
      //                                                                                    $("#submitAuthBtn").hide(); var invocationData = 
      { adapter : 
      "AuthenticationAdapter", procedure : 
      "submitAuthentication", parameters : [ username, password ] 
      }; WL.Client .invokeProcedure( invocationData, 
      { onSuccess : signOnSuccess, onFailure : signOnFailure 
      }); 
      });   connect.connect(dojo.byId(
      "logoutBtn"), 
      "onclick", null, function() 
      { require([ 
      "dijit" ], function(dijit) 
      { var currentView = dijit.byId(
      "LoginView").getShowingView(); var currentViewId = currentView.get(
      "id"); 
      
      if (currentViewId != 
      "LoginView") 
      { currentView .performTransition(
      "LoginView", 1, 
      "slide", 
      
      null); 
      } 
      }); WL.Client.logout(
      'AccountsAuthRealm'); 
      }); 
      }); 
      },   isLoginFormResponse : function(response) 
      { 
      
      if (!response || !response.responseJSON || response.responseText == 
      
      null) 
      { 
      
      return 
      
      false; 
      } 
      
      return response.responseJSON.authRequired; 
      },   onBeforeLogin : function(response, username, onSubmit, onCancel) 
      { 
      // clean up the input login fields document.getElementById(
      "usernameInputField").value = 
      ""; document.getElementById(
      "passwordInputField").value = 
      ""; document.getElementById(
      "AuthInfo").innerHTML = response.responseJSON.errorMessage; 
      },   onShowLogin : function() 
      {   require([ 
      "dijit" ], function(dijit) 
      { var currentView = dijit.byId(
      "LoginView").getShowingView(); var currentViewId = currentView.get(
      "id"); 
      
      if (currentViewId != 
      "LoginView") 
      { currentView .performTransition(
      "LoginView", 1, 
      "slide", 
      
      null); 
      } 
      }); 
      },   onHideLogin : function() 
      {   
      } 
      }; 
      }(); function getactivities(list,SEC_CODE,EMPLOYEE_CODE,EMPLOYEE_ID,FIRST_NAME,LAST_NAME,ORG_UNIT,POS_CODE,RN_DESCRIPTOR,RN_EMPLOYEE_USER_ID,response)
      { busyInd.show(); var invocationData = 
      { adapter : 
      "AuthenticateAdapterI", procedure : 
      "getActivityData", parameters : [list,EMPLOYEE_CODE,EMPLOYEE_ID,FIRST_NAME,LAST_NAME,ORG_UNIT,POS_CODE,RN_DESCRIPTOR,RN_EMPLOYEE_USER_ID,SEC_CODE] 
      };   WL.Client.invokeProcedure(invocationData, 
      { onSuccess : getActivitySuccess, onFailure : getAccountsError,   
      }); 
      } function signOnSuccess(response) 
      { 
      // alert('OnSuccess::'+response.invocationResult.HOME.RESPONSE.ERROR_DETAILS.ERR_CODE); 
      //WL.Client.login('AccountsAuthRealm'); 
      //alert(WL.Client.getLoginName("AccountsAuthRealm")); 
      //alert(WL.Client.isUserAuthenticated("AccountsAuthRealm")); userid =  response.invocationResult.HOME.REQUEST.USERID; resu = response.invocationResult.HOME.RESPONSE.ERROR_DETAILS.ERR_CODE; SEC_CODE=response.invocationResult.HOME.REQUEST.SECURE_KEY; EMPLOYEE_CODE=response.invocationResult.HOME.RESPONSE.DATA.DATA_ROW.EMPLOYEE_CODE; EMPLOYEE_ID=response.invocationResult.HOME.RESPONSE.DATA.DATA_ROW.EMPLOYEE_ID; FIRST_NAME=response.invocationResult.HOME.RESPONSE.DATA.DATA_ROW.FIRST_NAME; LAST_NAME=response.invocationResult.HOME.RESPONSE.DATA.DATA_ROW.LAST_NAME; ORG_UNIT=response.invocationResult.HOME.RESPONSE.DATA.DATA_ROW.ORG_UNIT; POS_CODE=response.invocationResult.HOME.RESPONSE.DATA.DATA_ROW.POS_CODE; RN_DESCRIPTOR=response.invocationResult.HOME.RESPONSE.DATA.DATA_ROW.RN_DESCRIPTOR; RN_EMPLOYEE_USER_ID=response.invocationResult.HOME.RESPONSE.DATA.DATA_ROW.RN_EMPLOYEE_USER_ID; 
      //alert(resu+user1+pass1); var invocationData = 
      { adapter : 
      "AuthenticationAdapter", procedure : 
      "submitAuthentication1", parameters : [ userid, EMPLOYEE_CODE, resu ] 
      }; WL.Client .invokeProcedure( invocationData, 
      { onSuccess : signOnSuccess122, onFailure : signOnFailure 
      }); 
      //     alert('DATA SEC_CODE::'+response.invocationResult.HOME.REQUEST.SECURE_KEY); 
      //   var authStatus=response.invocationResult.HOME.RESPONSE.ERROR_DETAILS.ERR_CODE; 
      //        if(authStatus==0){ 
      //         var userIdentity = { 
      //                               userId: username, 
      //                             displayName: username,  
      //                               attributes: {} 
      //              }; 
      //         WL.Server.setActiveUser("AccountsAuthRealm",userIdentity); 
      //          return { 
      //                   authRequired: false 
      //           }; 
      //getAccountsNew(SEC_CODE,EMPLOYEE_CODE,EMPLOYEE_ID,FIRST_NAME,LAST_NAME,ORG_UNIT,POS_CODE,RN_DESCRIPTOR,RN_EMPLOYEE_USER_ID); 
      //busyInd.hide(); 
      //  }     
      //else{ 
      //    WL.Logger.debug("Auth :: FAILURE"); 
      // return onAuthRequired(null, "Invalid login credentials"); 
      //} 
      }   function signOnSuccess122()
      { 
      //WL.Client.login('AccountsAuthRealm'); 
      //var au = WL.Client.getUserName("AccountsAuthRealm"); 
      //alert("user name from auth relam"+au); getAccountsNew(SEC_CODE,EMPLOYEE_CODE,EMPLOYEE_ID,FIRST_NAME,LAST_NAME,ORG_UNIT,POS_CODE,RN_DESCRIPTOR,RN_EMPLOYEE_USER_ID); 
      //busyInd.hide(); 
      } function signOnFailure(response) 
      { 
      //alert("An error occurred while authenticating from the server."); alert(
      "already logged in user, The application must logout first"); busyInd.hide(); 
      }
      


      this is my code and as u see i have defined variable at the start and after that i am calline adapter and set the variable but if i use the variable it is giving null value.Please check
      • SystemAdmin
        SystemAdmin
        2327 Posts
        ACCEPTED ANSWER

        Re: how to define global variable in worklight

        ‏2012-11-07T17:19:48Z  in response to SystemAdmin
        If you have have to use globals to achieve your goals you should re-evaluate your code.

        These are examples of global variables in JavaScript:

        
        var userid = 
        ""; var resu= 
        ""; function signOnSuccess(response) 
        { 
        }
        


        Why aren't you passing variables as parameters? and why so many functions?

        For example, here:

        
        function signOnSuccess(response) 
        {   [...]   WL.Client.invokeProcedure(invocationData,
        { onSuccess : signOnSuccess122, onFailure : signOnFailure
        });   
        }   function signOnSuccess122()
        { getAccountsNew(SEC_CODE,EMPLOYEE_CODE,EMPLOYEE_ID,FIRST_NAME,LAST_NAME,ORG_UNIT,POS_CODE,RN_DESCRIPTOR,RN_EMPLOYEE_USER_ID); 
        }
        


        You can write:

        
        function signOnSuccess(response) 
        {   
        //not sure why you don't group common things in a key-value pair var myData = 
        { secureKey : response.invocationResult.HOME.REQUEST.SECURE_KEY, employeeCode : response.invocationResult.HOME.RESPONSE.DATA.DATA_ROW.EMPLOYEE_CODE   
        }   WL.Client.invokeProcedure(invocationData,
        { onSuccess : function(res)
        { 
        //not sure why you're calling this adapter and not using the response getAccountsNew(myData);   
        }, onFailure : signOnFailure
        });   
        }
        


        I would re-think the code flow and re-write it.

        You may want to re-write functions as:

        
        var signOnSuccess = function(response) 
        { ... 
        };
        


        instead of:
        
        function signOnSuccess(response) 
        { ... 
        }
        


        So it feels more natural to pass them around like variables from function to function.