From 087f353af168f147c077326facc79cb776f80332 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 18 Nov 2014 15:07:14 -0800 Subject: [PATCH] Add app.utility to register utility functions by name --- lib/application.js | 15 +++++++++++++++ lib/loopback.js | 2 ++ 2 files changed, 17 insertions(+) diff --git a/lib/application.js b/lib/application.js index c66dd80e..27dc59c8 100644 --- a/lib/application.js +++ b/lib/application.js @@ -243,6 +243,21 @@ app.connector = function(name, connector) { this.connectors[camelize(name)] = connector; }; +/** + * Register a utility function with the given name + * + * The utilities will be used to resolve functions by name so that functions can + * be referenced in JSON configuration files, for example, the default value + * function, the middleware handler, or the validator + * @param {String} name The utility name + * @param {Function} fn The handler + */ +app.utility = function(name, fn) { + assert(typeof name === 'string', 'Utility function name must be a string'); + assert(typeof fn === 'function', 'The utility must be a function'); + this.utilities[name] = fn; +}; + /** * Get all remote objects. * @returns {Object} [Remote objects](http://apidocs.strongloop.com/strong-remoting/#remoteobjectsoptions). diff --git a/lib/loopback.js b/lib/loopback.js index 9d8351ce..6e2f6944 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -75,6 +75,8 @@ function createApplication() { app.connector('memory', loopback.Memory); app.connector('remote', loopback.Remote); + app.utilities = {}; + return app; }