Getting js Variables from outside into Lectora variables

lbryan-4843
lbryan-4843 Community Member Posts: 12 ☆ Roadie ☆
I have a js code (code.js) that reads data and is read at Lectora via html Extension (js file). Then I try to pass it to Lectora but with no success via a run js action.
For example a variable in the code.js I have a:
function GetFirstName() {
return "a-name";
}
which gets the FirstName field from a database. Then in Lectora I tried a run js action of:
lectora_Fname=GetFirstName();

I do have a lectora_Fname variable in Lectora, but data does not pass. Seems that scrips does pass the value to lectora_Fname inside the js action (its own realm seems) but not to the Lectora's variable lectora_Fname (seems to be in Lectora real).

How can I get a js from outside Lectora to equal the one captured in the js code?

Any suggestions would be greatly appreciated...
Thanks,
Louie

Comments

  • wheels
    wheels Community Member, Administrator, Moderator, Rockstar Manager Posts: 685 eLearning ROCKSTAR Admin Team
    You need to use the JavaScript variable name, which you can find in the Variable Manager on the Tools menu.

    In this case it would be Varlectora_Fname

    To set this use the set function.

    Varlectora_Fname.set( GetFirstName() );

    To get the value back use the getValue function.

    var fname = Varlectora_Fname.getValue();

    Hope this helps. If you are finding that issues with your functions being called you might need to define all the functions, and call all the functions, from the Display Window.
    var win = GetDisplayWindow();
    win.GetFirstName = function() { ... };
    
    var win = GetDisplayWindow();
    Varlectora_Fname.set( win.GetFirstName() ); 
  • CarlJFink
    CarlJFink Community Member, Lectora® Accessibility Group (LAUG) Member, eLearning Brothers® Partner Posts: 138 ♦ Idol ♦
    Joe, I have been suggesting for about 10 versions of Lectora now that you publish an actual API for JavaScript, instead of using the "oral tradition" method of passing it down to developers.
  • lbryan-4843
    lbryan-4843 Community Member Posts: 12 ☆ Roadie ☆
    Thanks Wheels...
    It worked beautifully from the extern folder (js file) in the html extension to the run js action with the code you suggested...variables come in great-from SQL to the js file via a trigger for a WinSvc.
    I also did the opposite (based on your great suggestion), from Lectora to a js file in another html extension which passes the information to a .txt file and from there a Data Load WinSvc takes the data into the SQL fields.

    Basically this works like a mini-LMS locally in a tablet. The only thing that I have to figure out is how to get to advance to next page [ trivNextPage(); ] right after the Web app service is triggered it sort of stops to show the page creation and the Lectora next page does not work to move on. I tried an *.asgx and shows a blank page with no heading. In Lectora I move on in my title execution by manually clicking on the next page which is the html extension that executes the js code.

    Anyway, thanks so much for your input...that's Guru type of answers!!!
  • jholland
    jholland Administrator, Rockstar Manager Posts: 95 eLearning ROCKSTAR Admin Team
    @lbryan-4843
    Here is a link to a Knowledge Base article that expands on what @wheels showed in his great answer:

    Containing your containers custom javascript suggestions
  • lbryan-4843
    lbryan-4843 Community Member Posts: 12 ☆ Roadie ☆
    One small problem has arise...while the extern LoadFromDB.js transfers the data from js variables to Lectora variables very nicely in "Preview" mode, when the title is published to offline the LoadFromDB.js is located in the same "Content" folder with the viewer and does not seem to pass the data. Even changing paths in a Sync Service.
    I changed to using API and the API works (checked with Postman) getting the data from the SQLite but I tried to use same philosophy as with the LoadFromDB.js to not success.
    The api is called in a js action with:

    var FirstName;
    function GetFirstName() {
    let request = new XMLHttpRequest();
    var uri = "http://localhost:5000/firstname/?tpatype=read&modid=1&lid=1"
    request.open("GET", uri);
    request.send();
    request.onload = () => {
    if (request.status === 200) {
    FirstName = request.responseText;
    //console.log(request.responseText);
    }
    else {
    console.log(`error ${request.status} ${request.statusText}`)
    }
    }
    }

    and
    in the HTML Extension I run:

    varlectora_FName.set(GetFirstName());

    and cannot get the value into the lectora_FName. I even tried to swap the codes, that is function into the HTML Extension and the bringing into Lectora [varlectora_FName.set(GetFirstName());] running in a js action, but no luck.
    Are there limitations to using APIs in Lectora and why something would work in Preview and not offline?
    My head now feels like an 8-ball.
    Thanks,
    Louie