-
Notifications
You must be signed in to change notification settings - Fork 421
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
Sorting a List doesn't update in a template correctly #1114
Conversation
I also experienced this issue |
We should add @index to #key. Sort should fire a remove and add event for the whole list. Sent from my iPhone
|
@justinbmeyer I've got the remove/add events working and covered by a test. But would like your help on the last failing test on pushing to a sorted list: |
…e-binded elements to reflect the sorted list correctly.
… trigger after list.sort().
…vents. Updated a test which I believe was mistakenly accepting this behavior.
I'm not sure what The |
@justinbmeyer I did get a little bit closer with the assumption that push() and unshift() should insert items into their sorted position. At this point it appears to work, except for one part: that |
Why would list need to support the 'move' event? Shouldn't an add event for the newly added item be enough? |
I tried that first, it required rewriting more of the sort module and broke more tests. It looked like it was closer to the original intent of the plugin to push the item onto the end, and then move it into the sorted position. Or so I had interpreted the _changes() function :)
|
…list is sorted already from an earlier direct call to sort(), or because items were pushed into it (which inserts them into their sorted position).
…d match "12" or "1." but not "1". And thus we'd only need to check the first char is a number.
…sent, not the new length of the list.
…sorted position. This fixes the failing tests on sort.
I managed to get sort working with the 'add' event, instead of going down the 'move' event approach - much easier. I had to change 2 tests, but I'm pretty sure they were incorrect in the first place, please see the commit messages. This is the reason the build fails: the compatibility test run is still executing these incorrect tests. |
Would like to get an opinion from someone on the CanJS team about the breaking "compatibility" test cases - I'm pretty sure they were incorrect in the first place. I think a sort plugin is important for anything beyond simple listing of data. For example, select lists, tables and data grids need to be able to control the ordering of list members. |
Functionally, I'm fine with |
I managed to get the view to update by triggering this._triggerChange("0", "remove", undefined, oldVal);
this._triggerChange("0", "add", newVal, oldVal); The downside to this is that those events are triggered to any user listener. Whatever logic they have occurring on those listeners will also occur when they sort. If there was a way to silently trigger those |
Want to ask about "move" event support for bindings in view. From performance point, what will be better? Moving existed DOM elements around or re-create them (remove/add flow)? |
I tried the solution, posted by @asavoy and it fixed data binding issue for sorted list. However I noticed current approach at the moment causes very significant performance issues. I have a big table, which I need to sort. Updating view via sort plugin and data binding cause significant delays. Basically all table DOM items are re-created, which takes few seconds in my case. I implemented DOM items reordering manually, to detach/append existed DOM items into correct places/order. As a result sorting without list binding works almost instantly for my big table. It made me think sort plugin should actually use "move" event, handled gracefully by view, to make data binding works efficiently in this case. |
This will be fixed via #1454 in 2.2. |
I'm trying to use
.comparator
and.sort()
as described in can.Map.prototype.sort to change the sorting of a can.List that is bound in a Mustache template.The can.List gets correctly sorted, but there appear to be a few bugs that prevent the template from reflecting the sorted data correctly.
Please refer to this JSFiddle:
list.sort()
won't update the rendered template at all.can.trigger(list, 'length')
orlist.push(newItem)
WILL force the update, but only for{{#key}}...{{/key}}
syntax. It's not the worst workaround, but I prefer to use{{#each}}
because that provides{{@index}}
.{{#each}}
syntax will never show list sorted, even after adding items to it.I've added tests that cover all of these issues.