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.
This commit is contained in:
Jarek Radosz 2020-02-04 11:42:25 +01:00 committed by GitHub
parent 9a52a44d09
commit 4ab696dd2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View File

@ -16,6 +16,7 @@ var define, requirejs;
inject: Ember.inject.controller
},
"@ember/object": {
action: Ember._action,
default: Ember.Object,
get: Ember.get,
getProperties: Ember.getProperties,

View File

@ -35,10 +35,14 @@ export default Component.extend({
});
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);
this.set("actions", connectorClass.actions);
},
willDestroyElement() {

View File

@ -1,5 +1,6 @@
import { acceptance } from "helpers/qunit-helpers";
import { extraConnectorClass } from "discourse/lib/plugin-connectors";
import { action } from "@ember/object";
const PREFIX = "javascripts/single-test/connectors";
acceptance("Plugin Outlet - Connector Class", {
@ -12,6 +13,26 @@ acceptance("Plugin Outlet - Connector Class", {
}
});
extraConnectorClass("user-profile-primary/hi", {
setupComponent() {
this.appEvents.on("hi:sayHi", this, this.say);
},
teardownComponent() {
this.appEvents.off("hi:sayHi", this, this.say);
},
@action
say() {
this.set("hi", "hi!");
},
@action
sayHi() {
this.appEvents.trigger("hi:sayHi");
}
});
extraConnectorClass("user-profile-primary/dont-render", {
shouldRender(args) {
return args.model.get("username") !== "eviltrout";
@ -25,6 +46,12 @@ acceptance("Plugin Outlet - Connector Class", {
<button class='say-hello' {{action "sayHello"}}></button>
<span class='hello-result'>{{hello}}</span>`
);
Ember.TEMPLATES[
`${PREFIX}/user-profile-primary/hi`
] = Ember.HTMLBars.compile(
`<button class='say-hi' {{action "sayHi"}}></button>
<span class='hi-result'>{{hi}}</span>`
);
Ember.TEMPLATES[
`${PREFIX}/user-profile-primary/dont-render`
] = Ember.HTMLBars.compile(`I'm not rendered!`);
@ -32,6 +59,7 @@ acceptance("Plugin Outlet - Connector Class", {
afterEach() {
delete Ember.TEMPLATES[`${PREFIX}/user-profile-primary/hello`];
delete Ember.TEMPLATES[`${PREFIX}/user-profile-primary/hi`];
delete Ember.TEMPLATES[`${PREFIX}/user-profile-primary/dont-render`];
}
});
@ -53,4 +81,7 @@ QUnit.test("Renders a template into the outlet", async assert => {
"hello!",
"actions delegate properly"
);
await click(".say-hi");
assert.equal(find(".hi-result").text(), "hi!", "actions delegate properly");
});