Created
October 10, 2013 15:12
-
-
Save tobiasoberrauch/6920044 to your computer and use it in GitHub Desktop.
jquery registry
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(){ | |
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