-
-
Save javajack/6974697 to your computer and use it in GitHub Desktop.
This file contains 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
function My_App() { | |
this.foo = '', | |
this.bar = '', | |
this.jQuery = false, | |
// Initializes our app. | |
this.init = function(params){ | |
// Set any custom app options. | |
for ( key in params ) this[key] = params[key]; | |
// Now we are going to possibly load jQuery if we need it. | |
this.loadjQuery(); | |
}, | |
// Possibly load jQuery. | |
this.loadjQuery = function(){ | |
// Store a reference of our object. | |
var self = this; | |
// If jQuery does not exist on the page, load it. | |
if ( window.jQuery === undefined ) { | |
var om = document.createElement('script'); | |
om.src = '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'; | |
om.onload = om.onreadystatechange = function(){ | |
var s = this.readyState; | |
if (s) if (s != 'complete') if (s != 'loaded') return; | |
try { | |
self.loadjQueryHandler(false); | |
} catch(e){} | |
}; | |
// Attempt to append it to the <head>, otherwise append to the document. | |
(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(om); | |
} else if ( window.jQuery.fn.jquery !== '1.10.2' ) { | |
// jQuery exists, but it is not the version we want, so we need to manage duplicate jQuery objects. | |
var om = document.createElement('script'); | |
om.src = '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'; | |
om.onload = om.onreadystatechange = function(){ | |
var s = this.readyState; | |
if (s) if (s != 'complete') if (s != 'loaded') return; | |
try { | |
self.loadjQueryHandler(true); | |
} catch(e){} | |
}; | |
// Attempt to append it to the <head>, otherwise append to the document. | |
(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(om); | |
} else { | |
// The version of jQuery loaded into the window is what we want to use, so we can just load the handler and output content. | |
self.jQuery = window.jQuery; | |
self.loadApp(); | |
} | |
}, | |
// Prepare to mitigate any conflicts on jQuery. | |
this.loadjQueryHandler = function(exists){ | |
// If jQuery already exists, don't overwrite the global but set noConflict to properly handle multiple versions. | |
if ( exists ) { | |
// Don't set global jQuery object - just store in our property. | |
this.jQuery = window.jQuery.noConflict(true); | |
this.loadApp(); | |
} else { | |
// Store the global jQuery object since it does not exist yet. | |
jQuery = window.jQuery.noConflict(true); | |
this.jQuery = jQuery; | |
this.loadApp(); | |
} | |
}, | |
// Load jQuery and fire off our app. | |
this.loadApp = function(){ | |
// Store a reference of our object. | |
var self = this; | |
// Woot, we now have jQuery! | |
this.jQuery(document).ready(function($){ | |
// Do cool jQuery stuff here by referencing $, e.g. $('#my_app_holder').html(my_app_response);. | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment