Created
December 3, 2010 03:28
-
-
Save quickredfox/726547 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
defineImportCallback = (script, callback) -> | |
unless script.readyState? | |
script.onload = -> callback() | |
return | |
# IE case | |
script.onreadystatechange = -> | |
if script.readyState is "loaded" or script.readyState is "complete" | |
script.onreadystatechange = null | |
callback() | |
importJs = (url, callback) -> | |
script = document.createElement "script" | |
script.type = "text/javascript" | |
defineImportCallback script, callback if callback | |
script.src = url | |
document.body.appendChild script | |
return | |
class JQueryImporter | |
constructor: (@version) -> | |
@jq = null | |
@whenReady = null | |
@isReady = false | |
invoke: (callback) -> | |
@setup() | |
@isolate callback | |
isolate: (callback) -> | |
return @whenReady = callback unless @isReady | |
@whenReady = null | |
callback @jq | |
setup: -> | |
unless document.body | |
return window.onload = => @setup | |
url = "http://ajax.googleapis.com/ajax/libs/jquery/#{@version}/jquery.min.js" | |
hadJquery = jQuery? | |
if hadJquery and jQuery.fn.jquery is @version | |
@isReady = true | |
@jq = jQuery | |
return | |
importJs url, => | |
@isReady = true | |
#this stores the new version and gives back the old one, completely. | |
if hadJquery | |
@jq = jQuery.noConflict(true) | |
else | |
@jq = jQuery | |
@isolate @whenReady if @whenReady? | |
# alert "beforie jqueryt is #{jQuery.fn.jquery}" | |
new JQueryImporter('1.4.4').invoke ($) -> | |
alert "go #{$.fn.jquery}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment