-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Accessing this
inside a template tag in a test context
#61
Comments
Relevant context from @NullVoxPopuli: This syntax:
is template-only syntax. Which uses this component manager, and has no getContext method https://github.com/glimmerjs/glimmer-vm/blob/main/packages/%40glimmer/runtime/lib/component/template-only.ts#L32 like the |
The key here, in my mind, is that it would be nice if it were possible to pass a context to |
Another potentially confusing thing here is that I believe the context setting for
|
I think one way forward, would be three phase:
Of note, something that would still work through this process, is the XState component manager: const Toggle = createMachine(...);
<template>
<Toggle as |blah|>
</Toggle>
</template> because you can't extend it in any way that would give you access to a This probably requires an RFC |
I think this is just a bug and doesn't need an RFC. In expression position, there's no reason
|
I don't really have an opinion on whether it needs an RFC, personally I also support/want this feature. However implementation wise it would require a bit more coordination than just what was laid out above, since I don't think just adding it to the locals array when calling the compiler would make it work, and arguably if it does work it's a bug just as much as if it allowed It isn't tremendously complicated to plumb that through on the Glimmer side, but that needs to happen. Alternatively, the variable can be rewritten a into a different name (e.g. At a high-level though, I agree with @ef4's general thinking that this is more appropriately modeled as a local variable capture in expression positions (i.e. arrow functions) rather than messing with the runtime component manger's getContext hook etc |
We also needed to do something similar to support private fields (don't remember if that has been RFC'ed either, but I think everyone thinks/assumed that should work) |
Poking more, this also requires a fix in ember-source or glimmer-vm. The template compiler itself is special-casing this and ignores |
(I commented before I saw @chancancode's updates, we are in alignment here.) |
Is the idea that we'd key ambient class FooComponent {
static {
template("{{this.foo}}", {
eval() { return eval(arguments[0]),
// are we saying that this flag makes the template a "constructor-style"
// template, which *does not* capture `this`?
component: this
})
}
// but this template (because it doesn't pass the `component` option,
// is an "arrow-style" template, which *does* capture `this`?
t = template("{{this.foo}}", { eval() { return eval(arguments[0]) })
} This seems perfectly sensible, and I think it works. That said, keying this behavior on whether |
Yes.
That would have been good feedback for RFC 931. But all this stuff shipped quite a long time ago, and I don't think this rises to the level of justifying ecosystem migration. |
In a class backed component a template tag has access to implicit
this
In a test context, a template tag does not have access to implicit
this
This difference in behavior can be confusing.
This has been talked about before on this PR #40
The text was updated successfully, but these errors were encountered: