This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/components/plugin-connector.js.es6
Jarek Radosz 4ab696dd2f
DEV: Add the @action decorator (#8836)
This also enables`@action` use in plugin connectors.

Setting `actions` earlier allows `setupComponents` to use them, for example, when setting up event listeners.
2020-02-04 11:42:25 +01:00

61 lines
1.7 KiB
JavaScript

import Component from "@ember/component";
import { defineProperty, computed } from "@ember/object";
import deprecated from "discourse-common/lib/deprecated";
import { buildArgsWithDeprecations } from "discourse/lib/plugin-connectors";
export default Component.extend({
init() {
this._super(...arguments);
const connector = this.connector;
this.set("layoutName", connector.templateName);
const args = this.args || {};
Object.keys(args).forEach(key => {
defineProperty(
this,
key,
computed("args", () => (this.args || {})[key])
);
});
const deprecatedArgs = this.deprecatedArgs || {};
Object.keys(deprecatedArgs).forEach(key => {
defineProperty(
this,
key,
computed("deprecatedArgs", () => {
deprecated(
`The ${key} property is deprecated, but is being used in ${this.layoutName}`
);
return (this.deprecatedArgs || {})[key];
})
);
});
const connectorClass = this.get("connector.connectorClass");
this.set("actions", connectorClass.actions);
for (const [name, action] of Object.entries(this.actions)) {
this.set(name, action);
}
const merged = buildArgsWithDeprecations(args, deprecatedArgs);
connectorClass.setupComponent.call(this, merged, this);
},
willDestroyElement() {
this._super(...arguments);
const connectorClass = this.get("connector.connectorClass");
connectorClass.teardownComponent.call(this, this);
},
send(name, ...args) {
const connectorClass = this.get("connector.connectorClass");
const action = connectorClass.actions[name];
return action ? action.call(this, ...args) : this._super(name, ...args);
}
});