Question

This is my bootstrap:

LanesBoard2010 = (function () {
    var appRoot = this;

    appRoot.deferredLoad = new Object(); //holding jqxhr states
    appRoot.utils = {}; //namespace for helper classes
    appRoot.models = {};
    appRoot.data = (function () { } ()); //store data from webservices here
    appRoot.app = (function () { } ()); //ViewModel
}();

After some initialisation i run my deferred app loader:

function appStart() {
    appRoot.deferredLoad.app = $.getScript('../TaskBoardContent/Script/app/lanesboard.js');
    $.when.apply($, appRoot.deferredLoad.app).then(function () {
        if (window.console)
            if (debug)
                console.log('application lanesdashboard was loaded successfully\n');
    }, function () {
        if (window.console)
            if (debug)
                console.log('fatal error: unable to load application lanesdashboard\n');
    });
};

Now i need to access appRoot, or more general, modify the properties of LanesBoard2010.

the following statement is from my lanesboard.js. It just fails after the first console.log, as i can see both variable names are unknown:

(function () {
    if (window.console)
        if (true) {
            console.log('App has been initialised successfully');
            console.log('Status of app access 1: ' + appRoot);
            console.log('Status of app access 2: ' + LanesBoard2010);

        }
}());

May sound stupid, but how can i safely access my variables? Is there any best practice? How would you do this?

Était-ce utile?

La solution

What exactly is your question? How you can prevent to use an undefined variable?

You can either define it before any calls and initialise it to null (var appRoot = LanesBoard2010 = null) or check the variable before you call console.log() (if (appRoot) console.log('Status of app access 1: ' + appRoot);).

I normally use the first suggestion. I first set all my caches to null, create the autocompletes, then use $.ajax to load them and when loaded, update the autocomplete sources.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top