From c9474ee04ec729d91198dbe8e395f1489b9a399a Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 17 Jun 2012 15:56:13 +0200 Subject: [PATCH] Ox.load: allow for passing array of modules --- source/Ox/js/Core.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/source/Ox/js/Core.js b/source/Ox/js/Core.js index 204ac411..47e80baa 100644 --- a/source/Ox/js/Core.js +++ b/source/Ox/js/Core.js @@ -96,30 +96,41 @@ Ox.Break = function() { Ox.BreakError = new SyntaxError('Illegal Ox.Break() statement'); /*@ -Ox.load Loads a module +Ox.load Loads one or more modules A module named "Foo" provides `Ox.Foo/Ox.Foo.js`, in which it defines one method, `Ox.load.Foo`, that takes two arguments, `options` and `callback`, and calls `callback` with one argument, `true` for success or `false` if an error occurred. Generally, the module should define `Ox.Foo` and attach its own methods there. - (module, callback) -> undefined + (module, callback) -> undefined (module, options, callback) -> undefined - (modules, callback) -> undefined + (modules, callback) -> undefined module Module name - modules Multiple modules {name: options, ...} options Module options + modules Multiple modules + Either `[moduleA, {moduleB: options}, ...]` + or `{moduleA: {}, moduleB: {options}, ...}` callback Callback function - success If true, the module has been loaded successfully + success If true, all modules have been loaded successfully @*/ Ox.load = function() { var callback = arguments[arguments.length - 1], - counter = 0, - isObject = Ox.isObject(arguments[0]), - length, - modules = isObject ? arguments[0] : {}, - success = 0; - if (!isObject) { - modules[arguments[0]] = Ox.isObject(arguments[1]) ? arguments[1] : {}; + counter = 0, length, modules = {}, success = 0, + type = Ox.typeOf(arguments[0]); + if (type == 'string') { + modules = Ox.extend( + {}, arguments[0], Ox.isObject(arguments[1]) ? arguments[1] : {} + ); + } else if (type == 'array') { + arguments[0].forEach(function(value) { + if (Ox.isString(value)) { + modules[value] = {}; + } else { + Ox.extend(modules, value) + } + }); + } else if (type == 'object') { + modules = arguments[0]; } length = Ox.len(modules); Ox.forEach(modules, function(options, module) {