From 7dd68e64d9248f760ded5162448cc8220f429cef Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 15 May 2018 16:01:51 +1000 Subject: [PATCH] FEATURE: allow plugins to modify class statics Adds `modifyClassStatic` to plugin api which can be used to amend static methods on a class --- .../discourse/lib/plugin-api.js.es6 | 60 ++++++++++++++----- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 index bd5be300f7f..ce784807708 100644 --- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 +++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 @@ -27,7 +27,7 @@ import { registerCustomAvatarHelper } from 'discourse/helpers/user-avatar'; import { disableNameSuppression } from 'discourse/widgets/poster-name'; // If you add any methods to the API ensure you bump up this number -const PLUGIN_API_VERSION = '0.8.20'; +const PLUGIN_API_VERSION = '0.8.21'; class PluginApi { constructor(version, container) { @@ -44,20 +44,7 @@ class PluginApi { return this.container.lookup('current-user:main'); } - /** - * Allows you to overwrite or extend methods in a class. - * - * For example: - * - * ``` - * api.modifyClass('controller:composer', { - * actions: { - * newActionHere() { } - * } - * }); - * ``` - **/ - modifyClass(resolverName, changes, opts) { + _resolveClass(resolverName, opts) { opts = opts || {}; if (this.container.cache[resolverName]) { @@ -72,7 +59,48 @@ class PluginApi { return; } - klass.class.reopen(changes); + return klass; + } + + /** + * Allows you to overwrite or extend methods in a class. + * + * For example: + * + * ``` + * api.modifyClass('controller:composer', { + * actions: { + * newActionHere() { } + * } + * }); + * ``` + **/ + modifyClass(resolverName, changes, opts) { + + const klass = this._resolveClass(resolverName, opts); + if (klass) { + klass.class.reopen(changes); + } + return klass; + } + + /** + * Allows you to overwrite or extend static methods in a class. + * + * For example: + * + * ``` + * api.modifyClassStatic('controller:composer', { + * superFinder: function() { return []; } + * }); + * ``` + **/ + modifyClassStatic(resolverName, changes, opts) { + + const klass = this._resolveClass(resolverName, opts); + if (klass) { + klass.class.reopenClass(changes); + } return klass; }