Created
January 26, 2015 14:26
-
-
Save phillipharding/9f5d3c175ab7e45e062e to your computer and use it in GitHub Desktop.
Load and Synchronise Execution of Multiple Scripts using Promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* load and synchronise execution of 2 or more scripts */ | |
var | |
upsuModulesLoaded = [ | |
RB.Masterpage.LoadResourceFromTenantRoot("_catalogs/masterpage/Buzz365/js/siteusage.js"), | |
RB.Masterpage.LoadResourceFromTenantRoot("_catalogs/masterpage/Buzz365/js/userprofile.js") | |
]; | |
$.when.apply($, upsuModulesLoaded) | |
.done(function() { | |
RB.Masterpage.Log("Masterpage>>userprofile.js and siteusage.js have loaded"); | |
var waitCount = 0; | |
function deferAndWaitForModuleExecution() { | |
/* this is an edge case: | |
while the scripts have been loaded they '**may**' not have executed yet, so we | |
may have to wait for this to happen before we can call the module init functions. | |
*/ | |
if (!RB.Masterpage.IsValidType('RB.Masterpage.Siteusage.EnsureSetup') || !RB.Masterpage.IsValidType('RB.Masterpage.Userprofile.EnsureSetup')) { | |
waitCount++; | |
if (waitCount > 10) | |
RB.Masterpage.UpSuModulesInitialised.reject(); /* give up after waiting for execution for 2 seconds */ | |
else { | |
RB.Masterpage.Log("Masterpage>>deferAndWaitForModuleExecution("+waitCount+") for USERPROFILE.JS and SITEUSAGE.JS initialisation"); | |
setTimeout(deferAndWaitForModuleExecution, 200); | |
} | |
return; | |
} | |
var moduleInits = [ | |
RB.Masterpage.Siteusage.EnsureSetup(), | |
RB.Masterpage.Userprofile.EnsureSetup() | |
]; | |
$.when.apply($, moduleInits) | |
.done(function() { | |
RB.Masterpage.UpSuModulesInitialised.resolve(); | |
}); | |
} | |
deferAndWaitForModuleExecution(); | |
}); | |
RB.Masterpage.UpSuModulesInitialised | |
.done(function() { | |
RB.Masterpage.Log("Masterpage>>userprofile.js and siteusage.js have initialised"); | |
RB.Masterpage.Siteusage.Acceptance(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment