Skip to content

Instantly share code, notes, and snippets.

@kitwalker12
Created August 15, 2013 01:14
Show Gist options
  • Save kitwalker12/6237406 to your computer and use it in GitHub Desktop.
Save kitwalker12/6237406 to your computer and use it in GitHub Desktop.
Clean way of making jQuery Initializers
// handle window.console if console.log isn't available. Define an empty function here
function emptyFunction() { }
if (window.console === undefined) {
window.console = {
log: emptyFunction,
error: emptyFunction,
info: emptyFunction,
trace: emptyFunction
};
}
// init namespace
window.app = {};
window.app.myapp = {};
//Following code can be put in other JS files which are included in the tree
(function ($, app) {
var $window = $(window), $nav = $('#nav');
app.home = {
fit: function() {
//Code to fit stuff on resize
},
nav: function() {
//Code for Nav features
}
};
if($('body#home').length > 0) {
app.home.fit();
$(window).on('resize', app.home.fit);
$(document).ready(app.home.fit);
$(document).ready(app.home.nav);
}
$(document).ready(function() {
//Other Initializers for plugins
});
}(jQuery, app.myapp));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment