I have an array of strings passed as an argument to a component, inside the component I am using “each” helper to render each string in a text input. I tried the following approach.
MainComponent.hbs
<Component @model={{model}}/>
//Eg: model.list = ["Jack", "Sparrow"];
Component.hbs
<div>
{{#each this.args.model.list as |name|}}
<div>
<PaperInput @value={{ name }}
@placeholder="Enter a Name"
@onChange={{action (mut name)}}/>
</div>
{{/each}}
</div>
I am running into the error “Uncaught (in promise) Error: Assertion Failed: You can only pass a path to mut”. Would really appreciate if anyone can let me know What’s going wrong here.
Thanks, this approach seems to be fine. But I am running into a problem with the statement this.args.model.list[index] = name; it does not change the array’s value and rerender the list. I am not finding the exact MutableArray method to accomplish this.
If it was an array cointaining objects the following would have worked
The point is: the Array should know itself has been changed. For that ember extends the native array with MutableArray mixin to provide such methods to correctly update/create/remove/insert items in an array.
For your case, suppose you have a list of names like: ['Jack', 'Sparrow'],then you should use an index as:
Thanks, I tried using ‘replace’ method. It indeed updates the array. But it makes the listr re render while I am still entering the text, causing the “input” element to lose the focus.
This, my friend, is not a problem of ember.js, but a problem of your ui interactions.
Just think about it carefully:
you use onChange as the hook to tell ember.js: “update the list whenever the input value has changed”
my code (which using ember.js) do exactly what you say: update the list whenever the input value has changed
so each time when a user types → the list gets updated → re-render → causing the input element loses its previous focus state
This is exactly what you tell ember.js to do, of course, it’s not what you desired to be.
So, what you really need is a confirmation mechanism, like an Update button. Then you should update the list when users click the button instead of in onChange hook.