Permalink
Browse files

Fixes #2530970 - Added static YUI.applyConfig to apply config setting…

…s to YUI.GlobalConfig in parts instead of in whole
  • Loading branch information...
1 parent 82b1c2d commit 523eb3576cc3f546585166d28eed9761e8652db5 @davglass davglass committed Nov 30, 2011
Showing with 118 additions and 2 deletions.
  1. +39 −0 sandbox/yui/config.html
  2. +3 −0 sandbox/yui/davglass.js
  3. +3 −0 sandbox/yui/foo.js
  4. +47 −2 src/yui/js/yui.js
  5. +26 −0 src/yui/tests/core-tests.js
@@ -0,0 +1,39 @@
+<!doctype html>
+<html>
+<head>
+</head>
+<body>
+
+<script src="../../build/yui/yui.js"></script>
+<script>
+
+YUI.applyConfig({
+ modules: {
+ davglass: {
+ fullpath: './davglass.js'
+ }
+ }
+});
+
+YUI.applyConfig({
+ modules: {
+ foo: {
+ fullpath: './foo.js'
+ }
+ }
+});
+
+YUI().use('davglass', function(Y) {
+ console.log(Y.config);
+});
+
+YUI().use('foo', function(Y) {
+ console.log(Y.config);
+});
+
+
+console.log(YUI.GlobalConfig);
+
+</script>
+</body>
+</html>
@@ -0,0 +1,3 @@
+YUI.add('davglass', function(Y) {
+ console.info('module davglass loaded..');
+});
View
@@ -0,0 +1,3 @@
+YUI.add('foo', function(Y) {
+ console.info('Module foo loaded..');
+});
View
@@ -264,6 +264,7 @@ proto = {
if (loader) {
loader._config(o);
}
+
},
/**
* Old way to apply a config to the instance (calls `applyConfig` under the hood)
@@ -1196,8 +1197,6 @@ Y.log('Fetching loader: ' + config.base + config.loaderPath, 'info', 'yui');
*/
};
-
-
YUI.prototype = proto;
// inheritance utilities are not available yet
@@ -1206,6 +1205,52 @@ Y.log('Fetching loader: ' + config.base + config.loaderPath, 'info', 'yui');
YUI[prop] = proto[prop];
}
}
+
+ /**
+Static method on the Global YUI object to apply a config to all YUI instances.
+It's main use case is "mashups" where several third party scripts are trying to write to
+a global YUI config at the same time. This way they can all call `YUI.applyConfig({})` instead of
+overwriting other scripts configs.
+@static
+@since 3.5.0
+@method applyConfig
+@param {Object} o the configuration object.
+@example
+
+ YUI.applyConfig({
+ modules: {
+ davglass: {
+ fullpath: './davglass.js'
+ }
+ }
+ });
+
+ YUI.applyConfig({
+ modules: {
+ foo: {
+ fullpath: './foo.js'
+ }
+ }
+ });
+
+ YUI().use('davglass', function(Y) {
+ //Module davglass will be available here..
+ });
+
+ */
+ YUI.applyConfig = function(o) {
+ if (!o) {
+ return;
+ }
+ //If there is a GlobalConfig, apply it first to set the defaults
+ if (YUI.GlobalConfig) {
+ this.prototype.applyConfig.call(this, YUI.GlobalConfig);
+ }
+ //Apply this config to it
+ this.prototype.applyConfig.call(this, o);
+ //Reset GlobalConfig to the combined config
+ YUI.GlobalConfig = this.config;
+ };
// set up the environment
YUI._init();
@@ -242,6 +242,32 @@ YUI.add('core-tests', function(Y) {
};
testY.use('editor');
},
+ test_global_apply_config: function() {
+ var Assert = Y.Assert,
+ test = this;
+
+ YUI.applyConfig({
+ modules: {
+ davglass: {
+ fullpath: './assets/davglass.js'
+ }
+ }
+ });
+
+ YUI.applyConfig({
+ modules: {
+ foo: {
+ fullpath: './assets/foo.js'
+ }
+ }
+ });
+
+ Assert.isObject(YUI.GlobalConfig, 'Global config not created');
+ Assert.isObject(YUI.GlobalConfig.modules, 'modules object in global config not created');
+ Assert.isObject(YUI.GlobalConfig.modules.davglass, 'First module in global config not created');
+ Assert.isObject(YUI.GlobalConfig.modules.foo, 'Second module in global config not created');
+
+ },
test_multiple_ua: function() {
var Assert = Y.Assert,
globalUA = YUI.Env.UA,

0 comments on commit 523eb35

Please sign in to comment.