New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
render() eventContext is not passed to event parts correctly when bound to same prototype function #1173
Comments
I think your example is incorrect? I simplified it a bit below. At line (1) the code is saying you want test('uses correct eventContext', () => {
function handler() { this.count++; }
const t = (context: Context) => html`<div @click=${handler}></div>`
const context1 = { count: 0 };
const context2 = { count: 0 };
render(t(context1), container, { eventContext: context1 });
render(t(context2), container, { eventContext: context1 }); // (1)
container.querySelector('div')!.click();
assert.equal(context1.count, 0);
assert.equal(context2.count, 1);
}); I.e., with the code in your example above, it should be: assert.equal(context1.count, 1);
assert.equal(context2.count, 0); |
No, I made a mistake in the code, and updated that. The second render is supposed to use context2, the idea is: both render call is binding to the same function, but with different eventContext. The reason is that, when rendering, the part would find the bound handler doesn't change, and the changed eventContext is not passed down to event part at the second render call, thus it would be invoked against old eventContext. |
Ok, thanks for the update. Can you describe your use case? The |
Alright, so my use case may be a bit complicated and not common, but I try my best to explain. So I have a custom element (not extending from LitElement), interface Controller {
readonly template: TemplateResult;
}
class PageContainer {
public controller: Controller | null = null;
} The purpose for @justinfagnani I understand the intended use case for The workaround for my use case is just to always use the form |
It's probably fair that We do document this fact in the guide:
It's also noted in the API docs in source, but it looks like these aren't being carried to the API docs on the website, so we'll look into that (filed as #1177). The workaround you came up with (re-binding the function every render) seems like a reasonable tradeoff. Changing the API to give a guarantee that any change to |
Description
When calling
render(template, container, { eventContext })
, if the event is bound to a function that belongs to a prototype (class member function), eveneventContext
is changed, the handler will still be invoked against the initialeventContext
Steps to Reproduce
src/test/lib/render_test.ts
Expected Results
The test case should pass
Actual Results
The test case fails
Browsers Affected
The text was updated successfully, but these errors were encountered: