locked
How can I make a class variable global'?

    Question

  • I've following function. I want to return one or more values from select caluse. This function is in a separate javascript file and I want to display returned value across different HTML navigation pages on the app. But how to make those values global?
    var UserName;
    (function () {
         var service = WinJS.Class.define(function () {
            that = this;
            that.db = null;
            that.path = getFullPath();
           },
    
     {
            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);
                          //that.username = row.username;
                          userName = row.username;
                          UserName = row.username;
                          //console.log("USER : " + UserName);
                          return UserName;
                      })
                  });
            }
        }
    }

    I used global var. UserName.. it is not working either. I'm creating a static variable inside this selectUser { } , but it is not working either.

    sonal

    Friday, September 5, 2014 6:47 PM

Answers

  • Hi SonalMac,

    You could use something as below as we can see from ApplicationData.LocalSettings | localSettings property:

    var applicationData = Windows.Storage.ApplicationData.current;
    
    var localSettings = applicationData.localSettings;
    
    // Create a simple setting
    
    localSettings.values["username"] = dbService.selectUser();
    
    // Read data from a simple setting
    
    var value = localSettings.values["username"];
            
    if (!value)
    {
        // No data
    }
    else
    {
        // Access data in value
    }
    
    // Delete a simple setting
    
    localSettings.values.remove("username");
    

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 19, 2014 9:05 AM
    Moderator

All replies

  • var UserName;
    (function () {
        
    var service = WinJS.Class.define(function () {
            that
    = this;
            that
    .db = null;
            that
    .path = getFullPath();
          
    },

    {
            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);
                         
    //that.username = row.username;
                          userName
    = row.username;
                         
    UserName = row.username;
                         
    //console.log("USER : " + UserName);
                         
    return UserName;
                     
    })
                 
    });
           
    }
       
    }
    }

    I want to send username to other pages from this file? I used global var. UserName.. it is not working either. I'm creating a static variable inside this selectUser { } , but it is not working either.


    sonal

    Sunday, September 7, 2014 6:18 AM
  • Hi SonalMac,

    A really really easy way to do this is to use local/temp object as WinJS.Application list.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 8, 2014 1:46 AM
    Moderator
  • I tried using local storage. It does not work either.

    In defatult.js,

    var

    storage = window.localStorage;

    storage.username = dbService.selectUser();

       

    In home.js,

    console.log(storage.username); //shows storage undefined. So I've to declare var storage here again, and it gives storage.username == undefined.


    sonal

    Tuesday, September 9, 2014 8:04 PM
  • Hi SonalMac,

    You could use something as below as we can see from ApplicationData.LocalSettings | localSettings property:

    var applicationData = Windows.Storage.ApplicationData.current;
    
    var localSettings = applicationData.localSettings;
    
    // Create a simple setting
    
    localSettings.values["username"] = dbService.selectUser();
    
    // Read data from a simple setting
    
    var value = localSettings.values["username"];
            
    if (!value)
    {
        // No data
    }
    else
    {
        // Access data in value
    }
    
    // Delete a simple setting
    
    localSettings.values.remove("username");
    

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 19, 2014 9:05 AM
    Moderator