[wip] hooks to patch pre/post initialized YUI instance #1590

Merged
merged 9 commits into from Mar 17, 2014
View
@@ -76,6 +76,19 @@ Y.Env.meta = META;
});
var out = loader.resolve(true);
+ * If the Loader needs to be patched before it is used for the first time, it
+ * should be done through the `doBeforeLoader` hook. Simply make the patch
+ * available via configuration before YUI is loaded:
+
+ YUI_config = YUI_config || {};
+ YUI_config.doBeforeLoader = function (config) {
+ var resolve = this.context.Loader.prototype.resolve;
+ this.context.Loader.prototype.resolve = function () {
+ // do something here
+ return resolve.apply(this, arguments);
+ };
+ };
+
* @constructor
* @class Loader
* @param {Object} config an optional set of configuration options.
@@ -102,6 +115,7 @@ Y.Env.meta = META;
* @param {Object} config.groups A list of group definitions. Each group can contain specific definitions for `base`, `comboBase`, `combine`, and accepts a list of `modules`.
* @param {String} config.2in3 The version of the YUI 2 in 3 wrapper to use. The intrinsic support for YUI 2 modules in YUI 3 relies on versions of the YUI 2 components inside YUI 3 module wrappers. These wrappers change over time to accomodate the issues that arise from running YUI 2 in a YUI 3 sandbox.
* @param {String} config.yui2 When using the 2in3 project, you can select the version of YUI 2 to use. Valid values are `2.2.2`, `2.3.1`, `2.4.1`, `2.5.2`, `2.6.0`, `2.7.0`, `2.8.0`, `2.8.1` and `2.9.0` [default] -- plus all versions of YUI 2 going forward.
+ * @param {Function} config.doBeforeLoader An optional hook that allows for the patching of the loader instance. The `Y` instance is available as `this.context` and the only argument to the function is the Loader configuration object.
*/
Y.Loader = function(o) {
@@ -166,6 +180,11 @@ Y.Loader = function(o) {
*/
self.context = Y;
+ // Hook that allows the patching of loader
+ if (o.doBeforeLoader) {
+ o.doBeforeLoader.apply(self, arguments);
+ }
+
/**
* Data that is passed to all callbacks
* @property data
@@ -2182,6 +2182,32 @@ YUI.add('loader-tests', function(Y) {
});
});
test.wait();
+ },
+ 'test loader patching mechanism': function() {
+ var loader,
+ hello,
+ out;
+
+ loader = new Y.Loader({
+ doBeforeLoader: function () {
+ var resolve = this.context.Loader.prototype.resolve;
+
+ this.context.Loader.prototype.resolve = function () {
+ hello = 'world';
+ return resolve.apply(this, arguments);
+ };
+ },
+ modules:{
+ 'foobar':{
+ fullpath: 'foo/bar.js'
+ }
+ },
+ require: ['foobar']
+ });
+
+ out = loader.resolve(true);
+ Assert.areEqual('foo/bar.js', out.js[0], 'Failed to monkey patch resolve()');
+ Assert.areEqual(hello, 'world', 'Failed to monkey patch resolve()');
}
});