This project is archived and is in readonly mode.

#1138 ✓wontfix
Yorick Peterse

Cloning collections produces unexpected results

Reported by Yorick Peterse | January 11th, 2011 @ 12:24 PM | in 1.3.1 (closed)

Earlier today I was working on a modal window system that itself was fairly simple. I noticed that whenever selection a particular (set) of elements using $$() the element would
be removed whenever using adopt, inject, and so on. Even when calling clone() (pretty much everywhere) the problem still occured.

With the help of keeto from IRC the issue was narrowed down to using clone() on a collection. An example of this issue can be found here: http://jsfiddle.net/SuKre/ . This is not necessarily an issue but it is rather unexpected that the elements are removed even though clone() is used on the elements.

Comments and changes to this ticket

  • Keeto

    Keeto January 11th, 2011 @ 12:29 PM

    Like Yorick, I was expecting Elements.clone to be Elements.clone :: [el] -> [el.clone()], returning a new collection containing the clones of the previous collection's elements, but the current Elements.clone is inheriting the original Array.clone because of the Array.prototype check in the Elements mirror.

    Perhaps we should force the clone method for Elements? The current behaviour is really unexpected...

  • Arieh

    Arieh January 11th, 2011 @ 12:50 PM

    I also think this should be an Elements method (so that the returned array will have elements methods as well).
    Adding a fix to cloneOf might also be a good idea (specifically adding an 'element' case) but it might break some code

  • Arian

    Arian January 11th, 2011 @ 02:42 PM

    • Milestone set to 1.3.1
    • State changed from “new” to “open”
    • Assigned user set to “Christoph Pojer”
    • Milestone order changed from “862” to “0”

    I agree that $$('div').clone() should return the cloned elements.

  • Christoph Pojer

    Christoph Pojer January 11th, 2011 @ 10:42 PM

    • State changed from “open” to “wontfix”

    Unfortunately we cannot do this because Array methods take precedence (think getLast and similar). As a solution you can use $$('div').map(Element.clone)

  • Yorick Peterse

    Yorick Peterse January 13th, 2011 @ 07:00 PM

    Wouldn't it in that case be better to automatically do that ($$('div').map(....)) in case the element on which the method is called is an array?

  • Christoph Pojer

    Christoph Pojer January 13th, 2011 @ 07:02 PM

    well the issue here is that some methods exist both on arrays and on elements. And array methods take precedence (in this case Array.clone over Element.clone)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Pages