-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Enable plain methods to work on classes when called from templates #21341
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
Draft
NullVoxPopuli
wants to merge
12
commits into
main
Choose a base branch
from
nvp/fix-this-binding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
609299d
Auto-bind prototype methods resolved via template path reads
NullVoxPopuli 4a212f3
Move test to gjs, test reference binding directly (not via {{on}})
NullVoxPopuli e733f57
Compile-time approach: new VM_GET_PROPERTY_BOUND_OP opcode
NullVoxPopuli 5465e5a
Defer property evaluation to invocation time
NullVoxPopuli 67a52d5
Use childRefFor transform instead of wrapper ComputeRef
NullVoxPopuli b30d27c
Fix all test failures for auto-bound this
NullVoxPopuli 3b23453
Fix type error: use Reflect.apply instead of CallableFunction.call
NullVoxPopuli 3e02660
Address review feedback
NullVoxPopuli 4ebeef2
Ref-tagging approach: preserve original value identity
NullVoxPopuli 87d8b52
Apply binding to all path reads, add #let block param test
NullVoxPopuli 4bcd303
Clean up: fix stale comment, stronger test assertions, import order
NullVoxPopuli 317aa5f
Add test: each over array of objects preserves this on item methods
NullVoxPopuli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
packages/@ember/-internals/glimmer/tests/integration/this-binding-test.gjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { RenderingTestCase, moduleFor, runTask } from 'internal-test-helpers'; | ||
| import { fn } from '@ember/helper'; | ||
| import { on } from '@ember/modifier'; | ||
| import { Component } from '../utils/helpers'; | ||
|
|
||
| moduleFor( | ||
| 'Path expression this-binding for class methods', | ||
| class extends RenderingTestCase { | ||
| ['@test this.foo maintains this binding'](assert) { | ||
| let instance; | ||
| let seenThis; | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| instance = this; | ||
| } | ||
|
|
||
| foo() { | ||
| seenThis = this; | ||
| } | ||
|
|
||
| <template><button type="button" {{on "click" this.foo}}>click me</button></template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.ok(instance, 'component instance was captured'); | ||
|
|
||
| runTask(() => this.$('button').click()); | ||
|
|
||
| assert.strictEqual( | ||
| seenThis, | ||
| instance, | ||
| '`this` inside the class method should be the component instance' | ||
| ); | ||
| } | ||
|
|
||
| ['@test this.obj.method maintains this binding through property chain'](assert) { | ||
| let innerInstance; | ||
| let seenThis; | ||
|
|
||
| class Inner { | ||
| constructor() { | ||
| innerInstance = this; | ||
| } | ||
|
|
||
| method() { | ||
| seenThis = this; | ||
| } | ||
| } | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| this.obj = new Inner(); | ||
| } | ||
|
|
||
| <template><button type="button" {{on "click" this.obj.method}}>click me</button></template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.ok(innerInstance, 'inner instance was captured'); | ||
|
|
||
| runTask(() => this.$('button').click()); | ||
|
|
||
| assert.strictEqual( | ||
| seenThis, | ||
| innerInstance, | ||
| '`this` inside the nested method should be the inner object instance' | ||
| ); | ||
| } | ||
| ['@test #let block param preserves this binding on method access'](assert) { | ||
| let innerInstance; | ||
| let seenThis; | ||
| let receivedArg; | ||
|
|
||
| class Inner { | ||
| constructor() { | ||
| innerInstance = this; | ||
| } | ||
|
|
||
| method(arg) { | ||
| seenThis = this; | ||
| receivedArg = arg; | ||
| } | ||
| } | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| this.obj = new Inner(); | ||
| } | ||
|
|
||
| <template> | ||
| {{#let this.obj as |o|}} | ||
| <button type="button" {{on "click" (fn o.method "did it")}}>click me</button> | ||
| {{/let}} | ||
| </template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.ok(innerInstance, 'inner instance was captured'); | ||
|
|
||
| runTask(() => this.$('button').click()); | ||
|
|
||
| assert.strictEqual( | ||
| seenThis, | ||
| innerInstance, | ||
| '`this` inside o.method should be the inner object instance' | ||
| ); | ||
| assert.strictEqual(receivedArg, 'did it', 'argument from fn is passed through'); | ||
| } | ||
|
|
||
| ['@test each over array of objects preserves this binding on item methods'](assert) { | ||
| let seenPairs = []; | ||
|
|
||
| class Item { | ||
| constructor(name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| greet() { | ||
| seenPairs.push({ self: this, name: this.name }); | ||
| } | ||
| } | ||
|
|
||
| let items = [new Item('alice'), new Item('bob'), new Item('carol')]; | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| this.items = items; | ||
| } | ||
|
|
||
| <template> | ||
| {{#each this.items as |item|}} | ||
| <button type="button" class={{item.name}} {{on "click" item.greet}}>{{item.name}}</button> | ||
| {{/each}} | ||
| </template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| runTask(() => this.$('button.alice').click()); | ||
| runTask(() => this.$('button.bob').click()); | ||
| runTask(() => this.$('button.carol').click()); | ||
|
|
||
| assert.strictEqual(seenPairs.length, 3, 'all three handlers fired'); | ||
| assert.strictEqual(seenPairs[0].self, items[0], 'alice: this is the Item instance'); | ||
| assert.strictEqual(seenPairs[0].name, 'alice', 'alice: this.name is correct'); | ||
| assert.strictEqual(seenPairs[1].self, items[1], 'bob: this is the Item instance'); | ||
| assert.strictEqual(seenPairs[1].name, 'bob', 'bob: this.name is correct'); | ||
| assert.strictEqual(seenPairs[2].self, items[2], 'carol: this is the Item instance'); | ||
| assert.strictEqual(seenPairs[2].name, 'carol', 'carol: this.name is correct'); | ||
| } | ||
| } | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exploration needed:
fnhas no involevment in binding / auto-bindingthisbecomes undefined