Created
October 20, 2013 15:14
-
-
Save op1ekun/7070938 to your computer and use it in GitHub Desktop.
YUI jquery + jquery plugins loader
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
// LOAD 3rd party libraries as commonJS modules | |
var module = { | |
exports : {} | |
}; | |
var exports = {}; | |
var config = (function() { | |
var BASE = '//localhost:8888/'; | |
return { | |
modules : { | |
'jquery' : { | |
fullpath : '//code.jquery.com/jquery-1.10.1.min.js', | |
requires : [ | |
'oop' | |
] | |
}, | |
'jquery-nicescroll' : { | |
// an empty file to fool the loader | |
fullpath : BASE + 'empty.js', | |
// the actual path to be used by jQuery get | |
getPath : '//cdnjs.cloudflare.com/ajax/libs/jquery.nicescroll/3.5.1/jquery.nicescroll.min.js', | |
requires : [ | |
'jquery' | |
] | |
} | |
}, | |
onProgress : function(e) { | |
// create dynamic, 3rd-party modules | |
switch(e.name) { | |
case 'jquery' : | |
(function() { | |
var clone; | |
YUI.add('jquery', function(Y) { | |
// create a clone ONLY once | |
if (!clone) { | |
clone = Y.clone(module.exports, true); | |
module.exports = {}; | |
} | |
Y.namespace('lib') | |
.jQuery = clone; | |
}, '1.10.1', { | |
requires : [ | |
'oop' | |
] | |
}); | |
})(); | |
break; | |
case 'jquery-nicescroll' : | |
var name = e.name, | |
path = config.modules[name].getPath; | |
YUI.add(e.name, function(Y) { | |
(function($) { | |
// uses absolute URLs | |
$.get(path, function(code) { | |
eval('[ function(jQuery) { ' + code + ' }][0]')(Y.lib.jQuery); | |
Y.Global.fire(name + '-loaded', Y); | |
// retrieve script as PLAIN TEXT | |
// get script doesn't work in sandbox | |
}, 'html'); | |
})(Y.lib.jQuery); | |
}, '2.0.3', { | |
requires : [ | |
'event-custom', | |
'jquery' | |
] | |
}); | |
break; | |
} | |
} | |
}; | |
})(); |
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
<!doctype html> | |
<html> | |
<head> | |
<script src="http://yui.yahooapis.com/3.4.0/build/yui/yui.js"></script> | |
<script src="config.js"></script> | |
<script> | |
YUI().use('event-custom', function(Y) { | |
// subscribe first | |
Y.Global.on('jquery-nicescroll-loaded', function(Y2) { | |
(function($) { | |
//jQuery with nicescroll plugin available | |
})(Y2.deps.jQuery); | |
}); | |
// then require the plugin | |
YUI(config).use('jquery-nicescroll', function(Y2) {}); | |
}); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment