Skip to content

Instantly share code, notes, and snippets.

@tobiasoberrauch
Created October 10, 2013 15:12
Show Gist options
  • Save tobiasoberrauch/6920044 to your computer and use it in GitHub Desktop.
Save tobiasoberrauch/6920044 to your computer and use it in GitHub Desktop.
jquery registry
(function(){
var Cache = function() {
var data = [];
};
Cache.prototype = {
set: function(key, value) {
this.data[key] = value;
},
get: function(key) {
return this.has(key) ? this.data[key] : null;
},
has: function(key) {
return key in this.data;
},
remove: function(key) {
delete this.data[key];
}
};
var cache = null;
var loadFile = function(filePath, callback){
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type= 'text/javascript';
script.onreadystatechange = function () {
if (this.readyState == 'complete') {
callback();
}
}
script.onload = callback;
script.src = filePath;
head.appendChild(script);
};
this.get = function(version) {
var cache = this.getCache();
if (cache.has(version)) {
jQuery = cache.get(version);
// set noConflict and return jquery
return jQuery.noConflict();
}
};
this.register = function(version, filePath) {
var cache = this.getCache();
loadFile(filePath, function() {
// write it into cache
cache.set(version, jQuery);
});
}
this.unregister = function(version) {
return this.getCache().remove(version);
}
this.getCache = function() {
if (cache === null) {
cache = new Cache();
}
return cache;
};
}).call(diy.core.namespace('jQueryRegistry'));
// IN APP
(function($){
// ...
// here are the apps source with $
// .....
})(jQueryRegistry.get('1.4.2'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment