locked
Fetching data from SQLite and making it global across navigation pages -- Is this asynchronous problem or global namespace problem?

    Question

  • I've following functions in dbData.js file. I want to use different functions from this file to different pages in my navigation app.

    First I want to select an existing user and display her name on home page. I can select name and display on console from the follwong selectUser function, but how can I access it on home.html page? I'm trying to use many different approaches like below. But nothing works.

    var UserName; //global var. (function () { var userData; var service = WinJS.Class.define(function () { that = this; that.db = null; that.path = getFullPath(); //that.username = { get: function () { that.selectUser(); } } //I tried this. It calls method. username: WinJS.Binding.as({ userData: that.selectUser() //this calls method as well. }) }, { selectUser: function () { SQLite3JS.openAsync(that.path) .then(function (myDb) { that.db = myDb; console.log("Service opened for select user"); return that.db.eachAsync("SELECT * FROM UserValues WHERE userid = 101", function (row) { console.log("USER : " + row.username + "ID : " + row.userid); //this prints right values, everytime. UserName = row.username; return UserName; }) .done(function () { UserName = UserName; console.log("user selected " + UserName); }, function (error) { console.log("Error selecting"); }) }); } } ); In home.js, I try to call this method many different ways. var dbService = new DBData.Service(); processed: function (element, options) { //UserName = dbService.username.get(); //var uname = (dbService.userData);

    console.log(dbService.userData); //The run is shown in below image.

    //dbService.username.uname = dbService.selectUser(); }

    I'm trying to call this function in processed method, but all these different approaches gives me "undefined" value. Like in following image. First it prints console.log as undefined and then opens the SQLite and then calls selectUser method.  The problem is the assignment is done first and after that the selectUser() method is called. Why so? What am I doing wrong?


    sonal


    • Edited by SonalMac Monday, September 08, 2014 6:00 PM
    Monday, September 08, 2014 5:58 PM

All replies

  • Hi Sonal,

    You can use WinJS.Namespace.define method to create your own namespace to organize your global variable. Define a new namespace in default.js like the following code.

    (function () {
        app.oncheckpoint = function (args) {
        };
        //define a new global variable
        WinJS.Namespace.define("YourCompany", {
            UserName: null,
        });
        app.start();
    })();
    


    Then you can set the UserName using SelectUser method in A.js file like this.

    YourCompany.UserName = dbService.selectUser();

    Get the value of UserName in B.js file like this.

    var loggedUser = YourCompany.UserName;

    You can also find more example from MSDN and StackOverflow.

    http://msdn.microsoft.com/en-us/library/windows/apps/hh967793.aspx.

    http://stackoverflow.com/questions/14159022/how-to-set-global-variables-in-javascript-html5-in-windows8-store-app.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 09, 2014 7:32 AM
    Moderator
  • Thanks for the reply. I did exactly same as said here, but it does not work either. It calls the console.log(loggerUser) first, giving it undefined value. After that it prints print statements from selectUser() function. I tried localStorage, it gives the same problem as well.  

    sonal

    Tuesday, September 09, 2014 8:06 PM
  • Hi SonalMac,

    Can you send me a repro project for test?

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 08, 2014 7:12 AM
    Moderator
  • Wednesday, October 08, 2014 8:42 PM