MithrilJS / mithril.js

A JavaScript Framework for Building Brilliant Applications
https://mithril.js.org
MIT License
13.99k stars 926 forks source link

keys misbehaving #639

Closed lhorie closed 7 years ago

lhorie commented 9 years ago

http://jsbin.com/litacu/1/edit?js,output

andr2 commented 9 years ago

Yes, and another one example https://jsfiddle.net/g89znqkr/19/

pelonpelon commented 9 years ago

A keyed list must not share its parent with siblings that are not part of the list. You'd never run into this in an HTML list unless done incorrectly (which is possible). But in the above examples we are not using HTML lists, we are just mapping over an array and stacking divs.

mithril.js uses parentElement.childNodes for indexing - L193

In Barney/Leo's example, table and a list of divs are under .root table throws off the count. Here is my solution: http://jsbin.com/honola/1/edit?js,output

And for Andre: https://jsfiddle.net/pelonpelon/g89znqkr/20/

This looks like something that should be made clear in the docs.

andr2 commented 9 years ago

@pelonpelon thank you for response and clarification! But can you explain why is it impossible to insert a keyed element at a right place?

pelonpelon commented 9 years ago

@andr2 As I look at your example again I realize I gave you a working solution but not the correct solution, which is: you can't do what you're trying to do.

m("span", { key: 'key' }, "privet") // parent is div#test m("button", { key: "adsf", onclick: function() {} }, "REDRAW") //parent is div

2 keyed list members must have the same parent.

And as indicated above, keyed list members cannot have non-keyed siblings (not applicable to your example).

Here's a possible solution, where div#test and button both have the same paerent: div, but I'm not sure I know what you're trying to do in the first place: https://jsfiddle.net/pelonpelon/g89znqkr/22/

dead-claudia commented 7 years ago

Closing - last v0.2 release has been cut.