Skip to content

A platform for community discussion. Free, open, simple.

DEV: New Plugin API method for delegating an app event

Robin Ward844c05b70b5d
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index 3e2bf32e060..fa3d98c4ba5 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -81,7 +81,7 @@ import { addSearchResultsCallback } from "discourse/lib/search";
import { addSearchSuggestion } from "discourse/widgets/search-menu-results";
// If you add any methods to the API ensure you bump up this number
-const PLUGIN_API_VERSION = "0.12.0";
+const PLUGIN_API_VERSION = "0.12.1";
class PluginApi {
constructor(version, container) {
@@ -1316,13 +1316,35 @@ class PluginApi {
* Add a suggestion shortcut to search menu panel.
*
* ```
- * addSearchSuggestion("in:assigned");
+ * api.addSearchSuggestion("in:assigned");
* ```
*
*/
addSearchSuggestion(value) {
addSearchSuggestion(value);
}
+
+ /**
+ * Calls a method on a mounted widget whenever an app event happens.
+ *
+ * For example, if you have a widget with a `key` of `cool-widget` that lives inside the
+ * `site-header` component, and you wanted it to respond to `thing:happened`, you could do this:
+ *
+ * ```
+ * api.dispatchWidgetAppEvent('site-header', 'cool-widget', 'thing:happened');
+ * ```
+ *
+ * In this case, the `cool-widget` must have a method called `thingHappened`. The event name
+ * is converted to camelCase and used as the method name for you.
+ */
+ dispatchWidgetAppEvent(mountedComponent, widgetKey, appEvent) {
+ this.modifyClass(`component:${mountedComponent}`, {
+ didInsertElement() {
+ this._super();
+ this.dispatch(appEvent, widgetKey);
+ },
+ });
+ }
}
// from http://stackoverflow.com/questions/6832596/how-to-compare-software-version-number-using-js-only-number