Skip to content

Instantly share code, notes, and snippets.

@toh82
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toh82/0fc9c0922d896f3c5f48 to your computer and use it in GitHub Desktop.
Save toh82/0fc9c0922d896f3c5f48 to your computer and use it in GitHub Desktop.
JS Module Launcher
(function ($) {
var moduleLauncher = {
oSettings: {
prefix: 'js_',
moduleRegister: [],
debug: false
},
/**
* Method to print out debug information
*
* @param {object} oObject
* @param {string} sMessage
* @private
*/
debug: function (sMessage, oObject) {
if (this.oSettings.debug) {
console.log(sMessage, oObject);
}
},
/**
*
* @param {string} sModuleName
* @param {object} oCallback
*/
registerModule: function (sModuleName, oCallback) {
var oModuleRegister = this.oSettings.moduleRegister,
bHasModule = $.inArray(sModuleName, oModuleRegister) != -1;
if(!bHasModule) {
oModuleRegister.push({
name: sModuleName,
callback: oCallback
});
}
},
launchModules: function () {
var oModuleRegister = this.oSettings.moduleRegister,
sModuleNamePrefix = this.oSettings.prefix;
$.each(oModuleRegister, function () {
var $module = $('.' + sModuleNamePrefix + this.name),
oCallback = this.callback,
oThis = this;
if ($module.length !== 0) {
oCallback.call(
oThis,
$module
);
}
});
}
};
moduleLauncher.registerModule(
'equalizeHeights',
function ($elements) {
$elements.equalizeHeights('equalize-heights-parent', true);
}
);
$(window).load(function () {
moduleLauncher.launchModules();
});
})(jQuery);
@homecoded
Copy link

Some ideas:

  • load the necessary scripts on-demand (only when they are needed)
  • make a backend-service (php-file) that can take a list of modules as query parameter and builds a output containing all the necessary js code (concats the scripts)
    • the backend service return-values will be cached by browser (no strain on the server)
    • one single call to load all the scripts from a page won't increase loading time a lot
    • only works well if we know what scripts to load on start of the page
       // this is how you could call the backend service
       $.getScript( "getScripts.php?modules=equalize-heights,magicslider,previewimage")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment