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

Support methods as actions ( use:obj.action ) #5395

Closed
pushkine opened this issue Sep 14, 2020 · 1 comment · Fixed by #5398
Closed

Support methods as actions ( use:obj.action ) #5395

pushkine opened this issue Sep 14, 2020 · 1 comment · Fixed by #5398

Comments

@pushkine
Copy link
Contributor

pushkine commented Sep 14, 2020

I'd like to suggest making methods valid actions that keep their contextual this, such as to make the following code valid :

<script>
	const obj = { 
		foo : "bar",
		action(element, { leet }) {
			console.log(this.foo); // "bar"
		},
	 }
</script>

<div use:obj.action={{ leet: 1337 }} />

It happens that some cross component functionality can only [is more convenient to] be applied through actions, in those situations one may want to bundle it all in an object or a class with action methods directly applying the relevant state.

Unfortunately methods must at least be destructured and optionally bound to the relevant object before being used, all in seemingly unnecessary boilerplate repeated across every component

While const obj_action = obj.action.bind(obj); in the script block or the class constructor is not the end of the world, it still acts on readability, performance, bundle size and memory consumption, not to mention the ReturnType of bind is always any in typescript

Supporting object methods as actions requires the following changes

  • Allow action functions to be a MemberExpression whose object and property are both Identifiers ( currently only accepts a single Identifier )
  • Pass MemberExpression.object in the block ctx and call the relevant property name on mount
- /* obj.action */ ctx[0].call(null, div, { leet: 1337 });
+ /* obj */        ctx[0].action(div, { leet: 1337 });
@Conduitry
Copy link
Member

Duplicate of #3935, although this one does have a better explanation.

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 a pull request may close this issue.

3 participants