Search code examples
global-variablesviewmodelhottowelsingle-page-application

Setting up global variable in SPA (Hot Towel)


I want to have a global variable that is accessible to modify in any view in my application. I am wondering what this the best way to do so when using hot towel. Currently, I am declareing this variable in my shell.js and referencing this in subsequent views. My shell.js returns the global variable. My other views have the shell defined at the top like define(['viewmodels/shell'], function(shell){} and I access the shell variable like shell.variablename. I am wondering if this is the best way of handling variables that need to be accessed by more than one view.


Solution

  • It can be done in many ways , either u create a config.js or any file or u can include the following code inside 'vm' object.

    // config.js

    define(function () {
    
        var variableName= 'BlahBlah';
    
        return {
            debugEnabled: ko.observable(true),
            variableName :variableName 
        };
    });
    

    // include this config.js in any javascript file

    define(['folderName/config'],
        function (config) {
                      var x=  config.variableName;
                          }
    

    This will work