Create a cheap way to proxy JavaScript module methods
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/f57f611c Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/f57f611c Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/f57f611c Branch: refs/heads/5.4-js-rewrite Commit: f57f611c333df612e5a9f0d3eb7c80c5118f65b6 Parents: ec0e316 Author: Howard M. Lewis Ship <[email protected]> Authored: Thu Jun 28 18:08:54 2012 -0700 Committer: Howard M. Lewis Ship <[email protected]> Committed: Thu Jun 28 18:08:54 2012 -0700 ---------------------------------------------------------------------- .../resources/org/apache/tapestry5/t5-console.js | 13 +---- .../main/resources/org/apache/tapestry5/t5-core.js | 40 +++++++++++++- 2 files changed, 38 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/f57f611c/tapestry-core/src/main/resources/org/apache/tapestry5/t5-console.js ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/resources/org/apache/tapestry5/t5-console.js b/tapestry-core/src/main/resources/org/apache/tapestry5/t5-console.js index fe500f5..bef8077 100644 --- a/tapestry-core/src/main/resources/org/apache/tapestry5/t5-console.js +++ b/tapestry-core/src/main/resources/org/apache/tapestry5/t5-console.js @@ -13,16 +13,5 @@ // limitations under the License. T5.define("console", function () { - - exports = {}; - - T5._.each(["debug", "info", "warn", "error"], function (name) { - exports[name] = function (message) { - require(["core/console"], function (cc) { - cc[name].call(null, message); - }); - }; - }); - - return exports; + return T5.proxyFunctionsToModule("core/console", "debug", "info", "warn", "error"); }); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/f57f611c/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js b/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js index a0c5c69..afc88e1 100644 --- a/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js +++ b/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js @@ -25,7 +25,7 @@ window.T5 = { /** _ is _.noConflict(), in other words, all Underscore functions are not inside * T5._, rather than simply _. */ - _ : _.noConflict() + _: _.noConflict() }; /** @@ -41,7 +41,7 @@ window.T5 = { * object * @returns the destination object */ -T5.extend = function(destination, source) { +T5.extend = function (destination, source) { var _ = T5._; if (_.isFunction(source)) { @@ -61,9 +61,43 @@ T5.extend = function(destination, source) { * object) * @return the namespace object */ -T5.define = function(name, source) { +T5.define = function (name, source) { var namespace = {}; T5[name] = namespace; return this.extend(namespace, source); }; + +/** + * Used when mapping a new-style module to an old-style namespace. + * @param moduleName name of module to export from. Additional arguments are the function names + * to proxy. + * @param functionNames... names of functions to to proxy + * @return An object with the function names; each value is a function that uses require() + * to obtain the function inside the module (asynchronously, at least the first time). + */ +T5.proxyFunctionsToModule = function (moduleName) { + + var slice = Array.prototype.slice; + var _ = T5._; + + var functionNames = _.tail(arguments); + var deps = [moduleName]; + var result = {} + + // Force the module to be loaded early, so that there will not (usually) be a delay + // later. + require(deps, function () { }); + + _.each(functionNames, function (name) { + result[name] = function () { + var capturedArguments = slice.call(arguments, 0); + + require(deps, function (module) { + module[name].apply(null, capturedArguments); + }); + }; + }); + + return result; +};
