Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created March 26, 2009 22:54
Show Gist options
  • Save isaacs/86422 to your computer and use it in GitHub Desktop.
Save isaacs/86422 to your computer and use it in GitHub Desktop.
// How awful is this approach? Really awful? Not awful?
// Cuz it seems not awful, and actually really convenient.
// Note how MyComponent has a single instance of YUI(),
// but without having to wrap the whole thing in the YUI call.
// This means that MyComponent can be extended later on,
// and still use the same single instance of YUI.
MyComponent = (function () {
var myYUI = null
var load = function (fn) {
if (myYUI && fn) return fn();
myYUI || YUI().use(
'log', 'lang', 'node', 'event', 'dom', 'json',
function (y) {
myYUI = y;
return fn();
});
return false;
};
var doSomething = function (asdf) {
if (!myYUI) throw new Error("Not loaded yet!");
// do something with the myYUI instance.
};
return { load:load, doSomething:doSomething };
})();
MyComponent.load(function () {
MyComponent.doSomething("foo");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment