DEV: Bind connector actions when made available under `this` (#18981)

Connector actions are already added as properties of the generated component, but they were not bound. Using them like `{{on "click" this.someAction"}}` and trying to access `this` would not work as expected. This commit binds all actions to the component generated component instance.
This commit is contained in:
David Taylor 2022-11-11 10:30:10 +00:00 committed by GitHub
parent 09b7433167
commit 6d126adb4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -51,7 +51,7 @@ export default Component.extend({
this.set("actions", connectorClass.actions); this.set("actions", connectorClass.actions);
for (const [name, action] of Object.entries(this.actions)) { for (const [name, action] of Object.entries(this.actions)) {
this.set(name, action); this.set(name, action.bind(this));
} }
const merged = buildArgsWithDeprecations(args, deprecatedArgs); const merged = buildArgsWithDeprecations(args, deprecatedArgs);

View File

@ -18,7 +18,7 @@ acceptance("Plugin Outlet - Connector Class", function (needs) {
extraConnectorClass("user-profile-primary/hello", { extraConnectorClass("user-profile-primary/hello", {
actions: { actions: {
sayHello() { sayHello() {
this.set("hello", "hello!"); this.set("hello", `${this.hello || ""}hello!`);
}, },
}, },
}); });
@ -53,6 +53,7 @@ acceptance("Plugin Outlet - Connector Class", function (needs) {
`${PREFIX}/user-profile-primary/hello` `${PREFIX}/user-profile-primary/hello`
] = hbs`<span class='hello-username'>{{model.username}}</span> ] = hbs`<span class='hello-username'>{{model.username}}</span>
<button class='say-hello' {{on "click" (action "sayHello")}}></button> <button class='say-hello' {{on "click" (action "sayHello")}}></button>
<button class='say-hello-using-this' {{on "click" this.sayHello}}></button>
<span class='hello-result'>{{hello}}</span>`; <span class='hello-result'>{{hello}}</span>`;
Ember.TEMPLATES[ Ember.TEMPLATES[
`${PREFIX}/user-profile-primary/hi` `${PREFIX}/user-profile-primary/hi`
@ -87,6 +88,12 @@ acceptance("Plugin Outlet - Connector Class", function (needs) {
"hello!", "hello!",
"actions delegate properly" "actions delegate properly"
); );
await click(".say-hello-using-this");
assert.strictEqual(
query(".hello-result").innerText,
"hello!hello!",
"actions are made available on `this` and are bound correctly"
);
await click(".say-hi"); await click(".say-hi");
assert.strictEqual( assert.strictEqual(