From 6a8303b004e1976739371431aa7358c672ad7313 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sun, 16 Sep 2012 21:45:10 +0200 Subject: added bootstrap --- module/web/static/js/utils/lazyRequire.js | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 module/web/static/js/utils/lazyRequire.js (limited to 'module/web/static/js/utils/lazyRequire.js') diff --git a/module/web/static/js/utils/lazyRequire.js b/module/web/static/js/utils/lazyRequire.js new file mode 100644 index 000000000..d20d78610 --- /dev/null +++ b/module/web/static/js/utils/lazyRequire.js @@ -0,0 +1,89 @@ +// Define the module. +define( + [ + "require" + ], + function( require ){ + + + // Define the states of loading for a given set of modules + // within a require() statement. + var states = { + unloaded: "UNLOADED", + loading: "LOADING", + loaded: "LOADED" + }; + + + // Define the top-level module container. Mostly, we're making + // the top-level container a non-Function so that users won't + // try to invoke this without calling the once() method below. + var lazyRequire = {}; + + + // I will return a new, unique instance of the requrieOnce() + // method. Each instance will only call the require() method + // once internally. + lazyRequire.once = function(){ + + // The modules start in an unloaded state before + // requireOnce() is invoked by the calling code. + var state = states.unloaded; + var args; + + var requireOnce = function( dependencies, loadCallback ){ + + // Use the module state to determine which method to + // invoke (or just to ignore the invocation). + if (state === states.loaded){ + loadCallback.apply(null, args); + + // The modules have not yet been requested - let's + // lazy load them. + } else if (state !== states.loading){ + + // We're about to load the modules asynchronously; + // flag the interim state. + state = states.loading; + + // Load the modules. + require( + dependencies, + function(){ + + args = arguments; + loadCallback.apply( null, args ); + state = states.loaded; + + + } + ); + + // RequireJS is currently loading the modules + // asynchronously, but they have not finished + // loading yet. + } else { + + // Simply ignore this call. + return; + + } + + }; + + // Return the new lazy loader. + return( requireOnce ); + + }; + + + // -------------------------------------------------- // + // -------------------------------------------------- // + + + // Return the module definition. + return( lazyRequire ); + + + } +); \ No newline at end of file -- cgit v1.2.3