From bf592413e12472bff295cc77cf474b0d7b7a790f Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 13 Dec 2013 16:45:31 -0800 Subject: [PATCH] Dedupe the alias methods during mixin To prevent the same method from showing up multiple times in API explorer, for example, Model.deleteById, Model.removeById --- lib/jutil.js | 22 +++++++++++++++++----- test/loopback-dl.test.js | 6 ++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/jutil.js b/lib/jutil.js index 6daa1134..33d81ea7 100644 --- a/lib/jutil.js +++ b/lib/jutil.js @@ -55,11 +55,12 @@ exports.mixin = function (newClass, mixinClass, options) { } if (options.staticProperties) { - Object.keys(mixinClass).forEach(function (classProp) { + var staticProxies = []; + Object.keys(mixinClass).forEach(function (classProp) { if (classProp !== 'super_' && classProp !== '_mixins' && (!newClass.hasOwnProperty(classProp) || options.override)) { var pd = Object.getOwnPropertyDescriptor(mixinClass, classProp); if(options.proxyFunctions && pd.writable && typeof pd.value === 'function') { - pd.value = exports.proxy(pd.value); + pd.value = exports.proxy(pd.value, staticProxies); } Object.defineProperty(newClass, classProp, pd); } @@ -68,11 +69,12 @@ exports.mixin = function (newClass, mixinClass, options) { if (options.instanceProperties) { if (mixinClass.prototype) { - Object.keys(mixinClass.prototype).forEach(function (instanceProp) { + var instanceProxies = []; + Object.keys(mixinClass.prototype).forEach(function (instanceProp) { if (!newClass.prototype.hasOwnProperty(instanceProp) || options.override) { var pd = Object.getOwnPropertyDescriptor(mixinClass.prototype, instanceProp); if(options.proxyFunctions && pd.writable && typeof pd.value === 'function') { - pd.value = exports.proxy(pd.value); + pd.value = exports.proxy(pd.value, instanceProxies); } Object.defineProperty(newClass.prototype, instanceProp, pd); } @@ -83,10 +85,20 @@ exports.mixin = function (newClass, mixinClass, options) { return newClass; }; -exports.proxy = function(fn) { +exports.proxy = function(fn, proxies) { + // Make sure same methods referenced by different properties have the same proxy + // For example, deleteById is an alias of removeById + proxies = proxies || []; + for(var i = 0; i