-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[BUGFIX] plain method calling from templates (no explicit binding required -- follows JS semantics) - on still requires binding
#21469
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
Open
NullVoxPopuli
wants to merge
4
commits into
main
Choose a base branch
from
nvp/fix-this-binding-for-real
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
96d5d1c
fix plain method calling
NullVoxPopuli c157426
Revert this-binding changes for (fn)
NullVoxPopuli c41a0c7
Revert this-binding changes for {{on}}
NullVoxPopuli 9c29b51
*actually* use JS bind semantics, and don't attempt to fix the proble…
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
326 changes: 326 additions & 0 deletions
326
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,326 @@ | ||
| import { set } from '@ember/object'; | ||
| import { RenderingTestCase, moduleFor, runTask } from 'internal-test-helpers'; | ||
| import { fn } from '@ember/helper'; | ||
| import { helperCapabilities, setHelperManager } from '@glimmer/manager'; | ||
| import { Component } from '../utils/helpers'; | ||
|
|
||
| moduleFor( | ||
| 'Automatic this-binding for class methods directly called from the template', | ||
| class extends RenderingTestCase { | ||
| ['@test class method'](assert) { | ||
| let instance; | ||
| let seenThis; | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| instance = this; | ||
| } | ||
|
|
||
| foo() { | ||
| seenThis = this; | ||
| } | ||
|
|
||
| <template>{{(this.foo)}}</template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.ok(instance); | ||
| assert.strictEqual(seenThis, instance); | ||
| } | ||
|
|
||
| ['@test maintains 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>{{ (this.obj.method) }}</template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.ok(innerInstance); | ||
| assert.strictEqual(seenThis, innerInstance); | ||
| } | ||
|
|
||
| ['@test replacing the base object uses correct `this`'](assert) { | ||
| let instance; | ||
| let seenThis; | ||
|
|
||
| class Inner { | ||
| method() { | ||
| seenThis = this; | ||
| } | ||
| } | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| instance = this; | ||
| this.obj = new Inner(); | ||
| } | ||
|
|
||
| <template>{{ (this.obj.method) }}</template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| let first = instance.obj; | ||
| assert.strictEqual(seenThis, first); | ||
|
|
||
| let second = new Inner(); | ||
| runTask(() => set(instance, 'obj', second)); | ||
|
|
||
| assert.strictEqual(seenThis, second); | ||
| } | ||
|
|
||
| ['@test a method passed as an argument stays bound to its original object'](assert) { | ||
| let instance; | ||
| let seenThis; | ||
|
|
||
| class Child extends Component { | ||
| <template>{{ (@cb) }}</template> | ||
| } | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| instance = this; | ||
| } | ||
|
|
||
| foo() { | ||
| seenThis = this; | ||
| } | ||
|
|
||
| <template><Child @cb={{this.foo}} /></template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.strictEqual(seenThis, 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|}} | ||
| {{(o.method "did it")}} | ||
| {{/let}} | ||
| </template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.ok(innerInstance); | ||
| assert.strictEqual(seenThis, innerInstance); | ||
| assert.strictEqual(receivedArg, 'did it', 'argument is passed through to the method'); | ||
| } | ||
|
|
||
| ['@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> | ||
| <ul> | ||
| {{#each this.items as |item|}} | ||
| <li>{{item.greet}}: {{item.name}}</li> | ||
| {{/each}} | ||
| </ul> | ||
| </template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.strictEqual(seenPairs.length, 3); | ||
| 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'); | ||
| } | ||
|
|
||
| ['@test already-bound functions are unaffected'](assert) { | ||
| let instance; | ||
| let seenThis; | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| instance = this; | ||
| } | ||
|
|
||
| foo = () => { | ||
| seenThis = this; | ||
| }; | ||
|
|
||
| <template>{{this.foo}}</template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.strictEqual(seenThis, instance); | ||
| } | ||
|
|
||
| ['@test (fn) on methods still behaves appropriately'](assert) { | ||
| let instance; | ||
| let seenThis; | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| instance = this; | ||
| } | ||
|
|
||
| foo = () => { | ||
| seenThis = this; | ||
| }; | ||
|
|
||
| <template> | ||
| {{#let (fn this.foo) as |fned|}} | ||
| {{ (fned) }} | ||
| {{/let}} | ||
| </template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.strictEqual(seenThis, instance); | ||
| } | ||
|
|
||
| ['@test a function with a custom helper manager read off a path keeps its manager'](assert) { | ||
| let sawDefinition; | ||
|
|
||
| class MyHelperManager { | ||
| capabilities = helperCapabilities('3.23', { hasValue: true }); | ||
|
|
||
| createHelper(definition, args) { | ||
| return { definition, args }; | ||
| } | ||
|
|
||
| getValue({ definition }) { | ||
| sawDefinition = definition; | ||
| return 'CUSTOM_MANAGER_RAN'; | ||
| } | ||
|
|
||
| getDebugName() { | ||
| return 'my-custom-helper'; | ||
| } | ||
| } | ||
|
|
||
| function customHelper() { | ||
| return 'PLAIN_FN_RAN'; | ||
| } | ||
| setHelperManager(() => new MyHelperManager(), customHelper); | ||
|
|
||
| class Inner { | ||
| customHelper = customHelper; | ||
| } | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| this.obj = new Inner(); | ||
| } | ||
|
|
||
| <template>{{(this.obj.customHelper)}}</template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| this.assertText('CUSTOM_MANAGER_RAN'); | ||
| assert.strictEqual( | ||
| sawDefinition, | ||
| customHelper, | ||
| // i.e.: a.foo !== a.foo.bind(a) | ||
| 'the custom manager received the original function, not a `.bind()` wrapper' | ||
| ); | ||
| } | ||
|
|
||
| ['@test a plain method passed through (fn) is not this-bound'](assert) { | ||
| let innerInstance; | ||
| let seenThis = 'UNSET'; | ||
|
|
||
| class Inner { | ||
| constructor() { | ||
| innerInstance = this; | ||
| } | ||
|
|
||
| method() { | ||
| seenThis = this; | ||
| } | ||
| } | ||
|
|
||
| class Demo extends Component { | ||
| constructor(...args) { | ||
| super(...args); | ||
| this.obj = new Inner(); | ||
| } | ||
|
|
||
| <template>{{#let (fn this.obj.method) as |f|}}{{(f)}}{{/let}}</template> | ||
| } | ||
|
|
||
| this.render(`<this.Demo />`, { Demo }); | ||
|
|
||
| assert.notStrictEqual(seenThis, 'UNSET', 'the method actually ran'); | ||
| assert.notStrictEqual( | ||
| seenThis, | ||
| innerInstance, | ||
| 'a plain method passed through `(fn)` is NOT bound to the object it was read from — `(fn)` value-passes the function and only the direct template call site binds' | ||
| ); | ||
| } | ||
| } | ||
| ); |
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.
more reference linking 🙈