Skip to content

Instantly share code, notes, and snippets.

@lsmith
Created January 10, 2012 05:03
Show Gist options
  • Select an option

  • Save lsmith/1587085 to your computer and use it in GitHub Desktop.

Select an option

Save lsmith/1587085 to your computer and use it in GitHub Desktop.
Allow class extensions to define which static properties to merge onto the built class from Y.Base.create/mix
// See ticket #2531567
// Use the current support for SuperClass._buildCfg.custom.{anyName} = handlerFunction to look for
// a new property Ext._buildCfg.buildFn on each class extension. If there, execute it. The extension
// then uses that function to manually migrate its static properties of interest.
// Two approaches are shown, but both rely on the Ext._buildCfg.buildFn static. This property and
// name are arbitrary. What matters is that the handlerFunction assigned to the superclass's
// _buildCfg.custom collection establishes the contract for where to find this attribute on each
// class extension.
// Two extensions for example purposes
function A() {}
A.staticFromA = function () { console.log('Static from A'); };
A._buildCfg = {
buildFn: function (HostClass, config) {
console.log('Ext A buildFn');
// Manually migrate a static property supplied by the extension (this)
HostClass.staticFromA = this.staticFromA;
}
};
function B() {}
B.staticFromB = function () { console.log('Static from B'); };
B._buildCfg = {
buildFn: function (HostClass, config) {
console.log('Ext B buildFn');
// Manually migrate a static property supplied by the extension (this)
HostClass.staticFromB = this.staticFromB;
}
};
// Approach #1, Add the new custom aggregator to Y.Base directly.
// CAVEAT: This will add a tiny bit of work to every call to Base.create and Base.mix
Y.Base._buildCfg.custom._ext = function (_, HostClass, Ext) {
var config = Ext._buildCfg,
buildFn = config && config.buildFn;
buildFn && buildFn.call(Ext, HostClass, config);
};
var C = Y.Base.create('c', Y.Base, [A, B]);
// Also works with Base.create('c', Y.Base, [A]); Base.mix(C, [B]);
C.staticFromA();
C.staticFromB();
var c = new C();
// Approach #2, insert an intermediate subclass of Base that includes the new custom aggregator:
var Intermediate = Y.extend(
function () { Y.Base.apply(this, arguments); },
Y.Base,
null, {
_buildCfg: {
custom: {
_ext: function (_, HostClass, Ext) {
var config = Ext._buildCfg,
buildFn = config && config.buildFn;
buildFn && buildFn.call(Ext, HostClass, config);
}
}
}
});
var C = Y.Base.create('c', Intermediate, [A, B]);
// Also works with Base.create('c', Y.Base, [A]); Base.mix(C, [B]);
C.staticFromA();
C.staticFromB();
var c = new C();
@rgrove

rgrove commented Jan 10, 2012

Copy link
Copy Markdown

Thanks for digging in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment