Skip to content

Instantly share code, notes, and snippets.

@scottbw
Created March 2, 2011 09:26
Show Gist options
  • Save scottbw/850684 to your computer and use it in GitHub Desktop.
Save scottbw/850684 to your computer and use it in GitHub Desktop.
This is a wrapper for Ryan Grove's LazyLoad enabling dynamic binding and exporting
/**
* Loader is a wrapper around LazyLoad that resolves a requested library, loads it, and then when loaded it
* fires a callback with the object exported by the library.
*
* Copyright (c) 2011 Scott Wilson <[email protected]>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @module loader
* @class loader
* @static
* @version 1.0
*/
/**
* The global object exports is used to hold exported library objects
*/
exports = {};
loader = (function(){
/**
* Resolves the requested library using the service registry
* TODO implement
* @param {String} name of library type required
* @return {String} URL of matching library
* @static
* @private
*/
function __resolve(library){
if (library=="events") return "../faye.js";
return "myscript.js";
// Here is what we should do...
// 1. Identify context data
// 2. Send request to Service Registry API
}
/**
* Loads the specified library and executes the specified
* callback (if any) when they have finished loading with
* the exported object
*
* @method load
* @param {String} name of library type required
* @param {Function} callback (optional) callback function to execute when
* the library has finished loading. This is executed with the exported object
* @static
* @private
*/
function __load(library, callback){
var scriptFile = __resolve(library);
LazyLoad.js(scriptFile, function(){
var exported = eval("exports."+library);
if (callback) callback(exported);
});
};
return {
/**
* Requests the specified library and executes the specified
* callback (if any) when they have finished loading.
*
* @method load
* @param {String} name of library type required
* @param {Function} callback (optional) callback function to execute when
* the library has finished loading. This is executed with the exported object
* @static
*/
load : function(library, callback) {
__load(library, callback);
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment