Getting rid of `.perform()` in JS
See original GitHub issuetl;dr
// I want
this.someTask('🥦');
this.someTask.performCount; // => 1
// instead of
this.someTask.perform('🥦');
this.someTask.performCount; // => 1
We already took the first step
This nifty trick allows users to just use the {{action}}
helper with tasks in their templates:
You’d use it as:
<button onclick={{action deleteUser user}}>
Delete {{user.name}}
</button>
This is super sexy. 🔥
The templates are agnostic to whether or not the thing that you’re passing is a regular (closure) action or a task.
I would love to have the equivalent in JS and get rid of .perform()
.
It’s already improved in JS
Since ES5 getters landed, ergonomics already improved massively for tasks, because instead of this:
export default Ember.Controller.extend({
someTask: task(function*() {
console.log('😐');
}),
actions: {
performSomeTask() {
this.get('someTask').perform();
}
}
});
You can know just go (Ember Twiddle):
export default Ember.Controller.extend({
someTask: task(function*() {
console.log('😍');
}),
actions: {
performSomeTask() {
this.someTask.perform();
}
}
});
One last step
But what’s still annoying is that, when you refactor a simple method to a task, you have to update all the places that call this method, because you need to switch from:
this.someMethod('🥦');
to:
this.someMethod.perform('🥦');
What would it take?
The TaskProperty
, when it is .get()
ed should not return an object that is a Task
, but instead should return a function that has all the Task
stuff on its prototype (?). There might be some mad science involved, but assuming that
a) this does not hurt performance
b) we do not remove .perform()
(for now) to avoid API churn
c) it works reliably
would you be open to this change? Then I might try my hand at a PR and see if this even works.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:8 (4 by maintainers)
Top GitHub Comments
The core problem was that the Ember observer / computed property system does not watch properties on functions. This means that
someTask.performCount
would be broken assomeTask
is a function.If we changed that in Ember core, then we could do this. 😄
Alright, actually got it working without any hackery: