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:
parent
9a52a44d09
commit
4ab696dd2f
|
@ -16,6 +16,7 @@ var define, requirejs;
|
||||||
inject: Ember.inject.controller
|
inject: Ember.inject.controller
|
||||||
},
|
},
|
||||||
"@ember/object": {
|
"@ember/object": {
|
||||||
|
action: Ember._action,
|
||||||
default: Ember.Object,
|
default: Ember.Object,
|
||||||
get: Ember.get,
|
get: Ember.get,
|
||||||
getProperties: Ember.getProperties,
|
getProperties: Ember.getProperties,
|
||||||
|
|
|
@ -35,10 +35,14 @@ export default Component.extend({
|
||||||
});
|
});
|
||||||
|
|
||||||
const connectorClass = this.get("connector.connectorClass");
|
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);
|
const merged = buildArgsWithDeprecations(args, deprecatedArgs);
|
||||||
connectorClass.setupComponent.call(this, merged, this);
|
connectorClass.setupComponent.call(this, merged, this);
|
||||||
|
|
||||||
this.set("actions", connectorClass.actions);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
willDestroyElement() {
|
willDestroyElement() {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { acceptance } from "helpers/qunit-helpers";
|
import { acceptance } from "helpers/qunit-helpers";
|
||||||
import { extraConnectorClass } from "discourse/lib/plugin-connectors";
|
import { extraConnectorClass } from "discourse/lib/plugin-connectors";
|
||||||
|
import { action } from "@ember/object";
|
||||||
|
|
||||||
const PREFIX = "javascripts/single-test/connectors";
|
const PREFIX = "javascripts/single-test/connectors";
|
||||||
acceptance("Plugin Outlet - Connector Class", {
|
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", {
|
extraConnectorClass("user-profile-primary/dont-render", {
|
||||||
shouldRender(args) {
|
shouldRender(args) {
|
||||||
return args.model.get("username") !== "eviltrout";
|
return args.model.get("username") !== "eviltrout";
|
||||||
|
@ -25,6 +46,12 @@ acceptance("Plugin Outlet - Connector Class", {
|
||||||
<button class='say-hello' {{action "sayHello"}}></button>
|
<button class='say-hello' {{action "sayHello"}}></button>
|
||||||
<span class='hello-result'>{{hello}}</span>`
|
<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[
|
Ember.TEMPLATES[
|
||||||
`${PREFIX}/user-profile-primary/dont-render`
|
`${PREFIX}/user-profile-primary/dont-render`
|
||||||
] = Ember.HTMLBars.compile(`I'm not rendered!`);
|
] = Ember.HTMLBars.compile(`I'm not rendered!`);
|
||||||
|
@ -32,6 +59,7 @@ acceptance("Plugin Outlet - Connector Class", {
|
||||||
|
|
||||||
afterEach() {
|
afterEach() {
|
||||||
delete Ember.TEMPLATES[`${PREFIX}/user-profile-primary/hello`];
|
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`];
|
delete Ember.TEMPLATES[`${PREFIX}/user-profile-primary/dont-render`];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -53,4 +81,7 @@ QUnit.test("Renders a template into the outlet", async assert => {
|
||||||
"hello!",
|
"hello!",
|
||||||
"actions delegate properly"
|
"actions delegate properly"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await click(".say-hi");
|
||||||
|
assert.equal(find(".hi-result").text(), "hi!", "actions delegate properly");
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue