Skip to content
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

feat: bind this context to owner #132

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

feat: bind this context to owner #132

wants to merge 1 commit into from

Conversation

buschtoens
Copy link

This PR binds this in functions invoked as a helper to the owner. I've suggested this behavior in emberjs/rfcs#756 (comment) and wanted to show a very rudimentary PoC.


this.setProperties({ lookup, testIdentifier });

await render(hbs`{{get (this.lookup this.testIdentifier) "string"}}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the intent is solid, but ultimately we need the VM to see property.path (when used in the invocation position), to do path.apply(property, args)

this way we get correctly bound functions on all objects.

like, XState interpreters, window globals, services, etc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! I've touched on that in my most recent comment in the RFC: emberjs/rfcs#756 (comment)

I also suggested not using the owner, but the invoking template context. I'd be very interested in your opinion on that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that would make sense to me.

the owner shouldn't get any special treatment with functions, but your functions should have whatever this you expect them to have.

so if your function needs the owner, you getOwner(this) in it.
but if your function is not defined in a place where this has an owner`, you won't get an owner.

function outerFunction() {
  getOwner(this); 
}

class extends Component {
  // can access owner
  myFunction = () => {
    let owner = getOwner(this);
  }
  
  outerFunction = outerFunction; // no owner
  boundOuterFunction = outerFunction.bind(this); // has owner
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants