I’m making a simple todo list and this one has stumped me completely. When I click on a task’s checkmark, I’d like for that element in the model to be moved to the bottom of the list. I found NO way to do this. THe change needs to be persisted to a firebase backend, or localStorage if I decide to swap out the firebase adapter with a localStorage adapter.
This doesn’t throw an error or anything, nothing happens. I tried a bunch of other ember and native javascript methods but nothing works. Haven’t found anything about this online. btw aside from trying to change the index of the elemtn the other two methods ni toggleTask() work fine
{{#each model as |task index|}}
<p {{action 'toggleTask' task}}>Some el</p>
{{/each}}
actions: {
toggleTask(task) {
task.toggleProperty('isDone');
this.get('model').removeObject(task); // remove it from its current position
this.get('model').addObject(task); // add it to the end
task.save(); // you definitely want some loading/error handling here
}
}
You can’t change the index of the task object directly. You instead want to change the model array. That causes the {{each}} to re-render.
(This solution only mutates the order of tasks in memory. I can’t tell from your example how to use your API to change the order of tasks on the server.)
Also, your prop argument is not what you think. It will be the value of task.isDone, not the name of the property.
Thanks but this doesn’t work because the model array is immutable so When I try this I get The result of a server query is immutable, to modify contents, use toArray()
I tried toArray() but not sure how to properly use it and how to update the model with it, everything I try returns an error. Surprised something so simple has proven so tricky!
Thanks but that only changes the array. Once the array is changed, how do you update the model with this new array? I tried something like this.set('model', newArray) and it changes the frontend correctly but does not persist to the store. I’m using firebase as a backend, while switching it out with the localStorage adapter.
Thank you for your help, When I first read your last reply it didn’t click for me because it didn’t show how to persist to the database. But after trying different things I realized that the computed property will always sort based on the property persisted to the database, so it actually does what I wanted. And instead of calling model from the template I can call sortedBands. Thanks guys for your help!