Skip to content

Instantly share code, notes, and snippets.

@ericf
Created March 12, 2010 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericf/330827 to your computer and use it in GitHub Desktop.
Save ericf/330827 to your computer and use it in GitHub Desktop.
/**
* Base Component Manager
*
* Oddnut Software
* Copyright (c) 2010 Eric Ferraiuolo - http://eric.ferraiuolo.name
*/
var ComponentMgr,
COMPONENTS = 'components',
E_INIT_COMPONENTS = 'initComponents',
L = Y.Lang,
isString = L.isString,
isFunction = L.isFunction,
noop = function(){};
// *** Constructor *** //
ComponentMgr = function (config) {
this.publish(E_INIT_COMPONENTS, {
defaultFn : this._defInitComponentsFn,
fireOnce : true
});
this.after('init', function(e){
this.fire(E_INIT_COMPONENTS, { components: [] });
});
};
// *** Static *** //
// *** Prototype *** //
ComponentMgr.prototype = {
// *** Instance Members *** //
// *** Public Methods *** //
getComponent : function () {
var args = Y.Array(arguments, 0, true),
components = args.slice(0, -1),
callback = isFunction(args[args.length-1]) ? args[args.length-1] : noop,
instances = [],
requires;
if (components.length < 1) {
callback.call(this);
return;
}
requires = this._getRequires(components);
if (requires.length > 0) {
Y.use.apply(Y, requires.concat(Y.bind(function(Y){
Y.Array.each(components, function(c){
instances.push(this._initComponent(c));
}, this);
callback.apply(this, instances);
}, this)));
} else {
Y.Array.each(components, function(c){
c = this._getComponent(c);
instances.push(c ? c.instance || null : null);
}, this);
callback.apply(this, instances);
}
},
// *** Private Methods *** //
_getComponent : function (c) {
var components = this[COMPONENTS];
return ( isString(c) && Y.Object.hasKey(components, c) ? components[c] : Y.Object.hasValue(c) ? c : null );
},
_getRequires : function (components) {
var requires = [];
Y.Array.each(components, function(c){
c = this._getComponent(c) || {};
requires = requires.concat(c.requires || []);
}, this);
return Y.Array.unique(requires);
},
_defInitComponentsFn : function (e) {
var components = e.components,
requires = this._getRequires(components);
Y.use.apply(Y, requires.concat(Y.bind(function(Y){
Y.Array.each(components, this._initComponent, this);
}, this)));
},
_initComponent : function (c) {
c = this._getComponent(c);
if ( ! c) { return null; }
if ( ! c.instance) {
var initFn = isFunction(c.initializer) ? c.initializer :
isString(c.initializer) && isFunction(this[c.initializer]) ? this[c.initializer] : noop;
try { c.instance = initFn.call(this); } catch(e){}
}
return c.instance || null;
}
};
Y.BaseComponentMgr = ComponentMgr;
YUI.add('mypage', function(Y){
function MyPage (config) {
MyPage.superclass.constructor.apply(this, arguments);
}
Y.extend(MyPage, Y.Base, {
components : {
myOverlay : {
requires : ['overlay', 'gallery-overlay-modal'],
initializer : function(){
return (new Y.Overlay({
width : '500px',
zInex : 100,
render : true,
plugins : [{ fn: Y.Plugin.OverlayModal }]
}));
}
},
mySlider : {
requires : ['slider'],
initializer : '_initMySlider'
}
},
initializer : function (config) {
Y.one('#foo').on('click', function(e){
this.getComponent('mySlider', function(mySlider){
// mySlider component had it's dependencies lazily loaded.
// mySlider component was lazily initialized.
// the instance returned by the initializer is cached,
// repeated calls just quickly return the instance to the callback.
mySlider.set('value', 400);
});
}, this);
Y.one('#bar').on('click', function(e){
this.getComponent('myOverlay', 'mySlider', function(myOverlay, mySlider){
// if mySlider isn't already initialized, then it will be by now.
myOverlay.show();
mySlider.set('value', 200);
});
}, this);
this.on('initComponents', function(e){
if (Y.one('#foo')) {
e.components.push('myOverlay');
// or: e.components.push(this.components.myOverlay);
}
});
},
_initMySlider : function () {
return (new Y.Slider({
min: 0,
max: 500,
length: 500,
render : true
}));
}
}, {
NAME : 'myPage'
});
Y.MyPage = Y.Base.mix(MyPage, [Y.BaseComponentMgr]);
}, '1', { requires: ['base', 'base-componentmgr'], optional: ['overlay', 'slider', 'gallery-overlay-modal'] });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment