I had created a little script a while back for grabbing necessary js
before executing designated functions, it has grown a bit and now is
as good as many developers might need for common uses. Here are a few
examples if it in action:
It allows you to dependancy preload assets before a Class's code is
initialized:
var eventApp = new Class({
Implements: [Events, Chain, Options],
Binds: ['validate','request','display'],
Uses: {js:['js/liquidSlider'] , css:['css/event_app']}, // Isn't
that just stupid-cool? :)
initialize: function(element, options) {
this.setOptions(options); ......and so on...
Next, Say you have a function called $UI and it has a space for a
content function, just add the .uses on the end of the function to
ensure that the js and css need for that content are there before
execution:
$UI({
id:appID,
type:'window',
height: 600,
width: 690,
minSize: {x:690, y:550},
content: function(){ new window[appID]($
(appID).instance.contentEl); }.uses({js:['js/slideshow'], css:['css/
slideshow']})
});
Lastly, you can wrap any block of js with $Uses:
$Uses({js:['js/common']}, function(){
pageControler = new pageControler({
pageSize: 15,
resultEl: $('result_list_ul'),
gMapEl: $('googleMap'),
historyManager : new WCHistory
({id:'histFrame',src:'blank.htm'})
});
pageControler.executeQuery($('txt_BusinessSearch').value,
true);
});
The dev team is coming out with another solution called Depender, but
this covers about 90% of the use cases with the following humble
amount code:
Function.implement({
uses:function(obj){
var self = this;
if(typeof obj == 'undefined'){return self;}
window.jsDict = (window.jsDict) ? window.jsDict : $$
('script').get('src'), window.cssDict = (window.cssDict) ?
window.cssDict : $$('link').get('href');
var h = $H(obj), jsd = window.jsDict.toString(), cssd =
window.cssDict.toString(), js = (h.get('js')) ? h.get('js').filter
(function(e){ return !jsd.contains(e); }) : [], css = (h.get('css')) ?
h.get('css').filter(function(e){ return !cssd.contains(e); }) : [],
bindWith = (obj.bindWith) ? obj.bindWith : window;
if(css.length > 0){
css.each(function(e){
new Asset.css(e + ((siteVersion) ? '.css?v=' +
siteVersion : '.css'));
window.cssDict.push(e);
});
}
if(js.length > 0){
var usesable = function(){
var count = 0, beforeStart = h.get('beforeStart') ||
function(){};
js.each(function(e){
count++;
new Asset.javascript(e + ((siteVersion) ? '.js?v=' +
siteVersion : '.js'),{ onload:function(){
count--;
window.jsDict.push(e);
if(count == 0){ count--;
beforeStart();
self.bind(bindWith)();
}
}});
});
}
return usesable;
}
else{ return self; }
}
});
Window.implement({
$Uses:function(obj, fn){
fn.uses(obj)();
}
});
Class.Mutators.Uses = function(uses){
return uses;
};