ember-set-helper
ember-set-helper copied to clipboard
[Feature request] Allow setting multiple keys
We have a few actions in our codebase that do only a setProperties
call which could use that set
helper but it accepts only one key and value. Perhaps another helper for multiple ones could be written? Or the same one to be extended to allow multiple keys?
Can you provide an example of the API that you're thinking about? Definitely interested, but I'm not sure what the ergonomics would be like.
@pzuraq - well, I haven't really thought about it, but I would guess something like:
<button {{on "click" (set-properties (array (key=this.greeting value="Hello!") (key=this.other value="bar!")))}}>
Or something. Another way would be to create a "chain" helper that would allow you to:
<button {{on "click" (chain (set this.greeting "Hello!") (set this.other "bar!"))}}>
Although this is not exactly the same because it will call set
multiple times which is different from setProperties
.
I guess there could be other ways of implementing this as well.
That chain
helper already exists as the queue
helper from ember-composable-helpers
https://github.com/DockYard/ember-composable-helpers#queue
You should able to do this:
<button {{on "click" (queue (set this.greeting "Hello!") (set this.other "bar!"))}}>
Or even this
<input {{on "input" (queue (set this.greeting (get _ 'target.value')) (set this.other (get _ 'target.value')))}}>
@alexlafroscia - true, but there's two issues with that - 1) I have to install yet another addon; 2) the problem that I've mentioned in my previous comment - it will call set multiple times which is different from setProperties
. Not ideal.
I believe setProperties
under the hood just calls set
multiple times, there isn't really a difference these days. There may have been in the past with observer/chains suspension, but that's not the case with the recent autotracking refactors.
FWIW, I'm about to merge a refactor to replace the main API here with the one from ember-simple-set-helper
, and going to release a new major version. The recommendation for picking values from events (e.g. the event.target.value
for inputs) is going to be using {{pick}}
from ember-composable-helpers
, so it may make sense to use it together with this helper for more than one reason.
That said, I'm still open to adding something like this, but would need to think on the API a bit more to see if it would be worth it from an ergonomics perspective.