From beac81d0ee67e08178d290eb1152611aa02df424 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 3 Nov 2016 11:52:30 -0400 Subject: [PATCH] Support both `_actions` and `actions` for delegating --- .../components/topic-footer-buttons.js.es6 | 13 +++---------- .../discourse/mixins/delegated-actions.js.es6 | 16 ++++++++++++++++ .../javascripts/discourse/widgets/widget.js.es6 | 9 +++++++-- app/assets/javascripts/main_include.js | 1 + 4 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 app/assets/javascripts/discourse/mixins/delegated-actions.js.es6 diff --git a/app/assets/javascripts/discourse/components/topic-footer-buttons.js.es6 b/app/assets/javascripts/discourse/components/topic-footer-buttons.js.es6 index d9aad34dccf..aeb9db8baab 100644 --- a/app/assets/javascripts/discourse/components/topic-footer-buttons.js.es6 +++ b/app/assets/javascripts/discourse/components/topic-footer-buttons.js.es6 @@ -1,6 +1,7 @@ import computed from 'ember-addons/ember-computed-decorators'; +import DelegatedActions from 'discourse/mixins/delegated-actions'; -export default Ember.Component.extend({ +export default Ember.Component.extend(DelegatedActions, { elementId: 'topic-footer-buttons', // Allow us to extend it @@ -8,15 +9,7 @@ export default Ember.Component.extend({ init() { this._super(); - - this._actions = this._actions || {}; - - (this.get('topicDelegated') || []).forEach(m => { - this._actions[m] = function() { - this.sendAction(m); - }; - this.set(m, m); - }); + this.delegateAll(this.get('topicDelegated')); }, @computed('topic.details.can_invite_to') diff --git a/app/assets/javascripts/discourse/mixins/delegated-actions.js.es6 b/app/assets/javascripts/discourse/mixins/delegated-actions.js.es6 new file mode 100644 index 00000000000..8940c1d49bf --- /dev/null +++ b/app/assets/javascripts/discourse/mixins/delegated-actions.js.es6 @@ -0,0 +1,16 @@ + +export const TARGET_NAME = (Ember.VERSION[0] === "2") ? 'actions' : '_actions'; + +export default Ember.Mixin.create({ + + delegateAll(actionNames) { + actionNames = actionNames || []; + + this[TARGET_NAME] = this[TARGET_NAME] || {}; + + actionNames.forEach(m => { + this[TARGET_NAME][m] = function() { this.sendAction(m); }; + this.set(m, m); + }); + } +}); diff --git a/app/assets/javascripts/discourse/widgets/widget.js.es6 b/app/assets/javascripts/discourse/widgets/widget.js.es6 index bef20bf04e6..851ad09bd90 100644 --- a/app/assets/javascripts/discourse/widgets/widget.js.es6 +++ b/app/assets/javascripts/discourse/widgets/widget.js.es6 @@ -1,6 +1,11 @@ -import { WidgetClickHook, WidgetClickOutsideHook, WidgetKeyUpHook, WidgetKeyDownHook, WidgetDragHook } from 'discourse/widgets/hooks'; +import { WidgetClickHook, + WidgetClickOutsideHook, + WidgetKeyUpHook, + WidgetKeyDownHook, + WidgetDragHook } from 'discourse/widgets/hooks'; import { h } from 'virtual-dom'; import DecoratorHelper from 'discourse/widgets/decorator-helper'; +import { TARGET_NAME } from 'discourse/mixins/delegated-actions'; function emptyContent() { } @@ -266,7 +271,7 @@ export default class Widget { if (target) { // TODO: Use ember closure actions - const actions = target._actions || target.actionHooks || {}; + const actions = target[TARGET_NAME] || target.actionHooks || {}; const method = actions[actionName]; if (method) { promise = method.call(target, param); diff --git a/app/assets/javascripts/main_include.js b/app/assets/javascripts/main_include.js index 96b3abcd9d3..c18248dffbf 100644 --- a/app/assets/javascripts/main_include.js +++ b/app/assets/javascripts/main_include.js @@ -25,6 +25,7 @@ //= require ./discourse/lib/formatter //= require ./discourse/lib/eyeline //= require ./discourse/mixins/scrolling +//= require ./discourse/mixins/scrolling //= require ./discourse/models/model //= require ./discourse/models/rest //= require ./discourse/models/badge-grouping