Assign local variable in OnInitialize function of mobile app

Assign local variable in OnInitialize function of mobile app

  

Hi.

I have been trying to assign a local variable to store a token value, which is retrieved from a javascript call. Unfortunately, the first time this is called, the local variable returns a blank value. However on running it again, the value appears. How does this work?

It's difficult to know, without knowing what the script is doing. Does it require the DOM to be loaded? In this case, you can't use the Initialize event (use Ready instead). Take a look at the Programming Model section of the Developing OutSystems Mobile Apps course for more details.


Hi Saurav,

As Paulo wrote, it is important to understand the Screen and Block Lifecycle Events to understand when events are handled. In your case, if your JavaScript is requiring something that was not already evaluated that could be the reason of the Undefined value you get.

The OnInitialize event of a screen is the first event to run and until it isn't concluded no other event can happen.

If you don't figure out what is happening, can you share more info or even your oml?


Cheers,

António Pereira

António Pereira wrote:

Hi Saurav,

As Paulo wrote, it is important to understand the Screen and Block Lifecycle Events to understand when events are handled. In your case, if your JavaScript is requiring something that was not already evaluated that could be the reason of the Undefined value you get.

The OnInitialize event of a screen is the first event to run and until it isn't concluded no other event can happen.

If you don't figure out what is happening, can you share more info or even your oml?


Cheers,

António Pereira

I'm using the OnInitialize to add a script to the <head> tag in the following fashion:

var script_src = 'https://c64.assets-yammer.com/assets/platform_js_sdk.js';
var head = document.getElementsByTagName('head')[0];
var new_script = document.createElement('script');
new_script.type = 'text/javascript';
new_script.src= script_src;
new_script.setAttribute('data-app-id', 'xxyyzz');
head.appendChild(new_script);

After this, I'm using OnReady to perform a login to Yammer in the following way:

yam.getLoginStatus(
  function(response) {
    if (response.authResponse) {
      console.log("logged in");
      $parameters.Token = response.access_token.token;
      console.dir(response); //print user information to the console
      console.dir($parameters.Token + "  1");
    }
    else {
      yam.platform.login(function (response) { //prompt user to login and authorize your app, as necessary
        if (response.authResponse) {
          $parameters.Token = response.access_token.token;
          console.dir(response); //print user information to the console
          console.dir($parameters.Token + "  2");
        }
      });
    }
  }
);

I'm trying to assign a local variable to the token that is returned. I have an expression DOM to show this value, which remains blank. (The console shows the value though)

I'm getting the error: "yam" not defined. (Built in yammer call which requires the script to run)

When I add this JS to a button Onclick, it assigns the local variable and shows it in the expression DOM. (After 2 clicks. 1 does not work for some reason)


Solution

var script_src = 'https://c64.assets-yammer.com/assets/platform_js_sdk.js'; var head = document.getElementsByTagName('head')[0]; var new_script = document.createElement('script'); new_script.type = 'text/javascript'; new_script.src= script_src; new_script.setAttribute('data-app-id', 'xxyyzz'); head.appendChild(new_script);

This code shouldn't be placed in the OnInitialize since there isn't a screen DOM when this event happens. You can use the onReady event instead.

Regarding the rest of the code,

yam.getLoginStatus(
  function(response) {
    if (response.authResponse) {
      console.log("logged in");
      $parameters.Token = response.access_token.token;
      console.dir(response); //print user information to the console
      console.dir($parameters.Token + "  1");
    }
    else {
      yam.platform.login(function (response) { //prompt user to login and authorize your app, as necessary
        if (response.authResponse) {
          $parameters.Token = response.access_token.token;
          console.dir(response); //print user information to the console
          console.dir($parameters.Token + "  2");
        }
      });
    }
  }
);

A server call is async. Try to add a $resolve() to the last line of your function(response) code as indicated in OutSystems documentation.

Solution