From d955af59600516cdc14a3bb940fc58dc28896392 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 30 Oct 2017 16:14:20 -0400 Subject: [PATCH] FEATURE: Allow widgets to call `_super()` when reopened --- .../javascripts/discourse/widgets/widget.js.es6 | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/widgets/widget.js.es6 b/app/assets/javascripts/discourse/widgets/widget.js.es6 index 022625168f4..f5822f8fa9b 100644 --- a/app/assets/javascripts/discourse/widgets/widget.js.es6 +++ b/app/assets/javascripts/discourse/widgets/widget.js.es6 @@ -133,7 +133,21 @@ export function reopenWidget(name, opts) { opts.html = opts.template; } - Object.keys(opts).forEach(k => existing.prototype[k] = opts[k]); + Object.keys(opts).forEach(k => { + let old = existing.prototype[k]; + + if (old) { + // Add support for `this._super()` to reopened widgets if the prototype exists in the + // base object + existing.prototype[k] = function(...args) { + let ctx = Object.create(this); + ctx._super = (...superArgs) => old.apply(this, superArgs); + return opts[k].apply(ctx, args); + }; + } else { + existing.prototype[k] = opts[k]; + } + }); return existing; }