Skip to content

Loading…

nested dom-repeat with sort attribute shows duplicate entries when adding new items. #1744

Closed
andrepl opened this Issue · 7 comments

3 participants

@andrepl

When pushing new items into a nested array rendered by a dom-repeat with a sort attribute, duplicate items are rendered:

http://jsbin.com/fazazujamu/2/edit

(Click 'go' once, you should see items 11 and 21 duplicated in the output)

from my testing, this requires the array to be nested inside another array which is rendered with dom-repeat, and it requires the items to be added one by one, AFTER the parent item is already rendered, and must be added using this.push.

@andrepl andrepl changed the title from nested dom-repeat with sort at attribute shows duplicate entries when adding new items. to nested dom-repeat with sort attribute shows duplicate entries when adding new items.
@kevinpschaaf
Owner

I fixed this in your last jsBin also:

Original:

        go: function () {
            for (var i=0; i<DATA.length; i++) {
                var items = [];
              var cat = {'num': DATA[i].num, items: items};
                this.push('categories', cat);                     
                for (var j=0; j<DATA[i].items.length; j++) {
                    // `i` here is only ever 0 or 1, so you're always pushing new items
                    // into categories[0].items or categories[1].items
                    this.push('categories.' + i + '.items', DATA[i].items[j]);
                }

            }
        }

In the inner loop, i is only ever 0 or 1, so you're always pushing into the this.categories[0].items or this.categories[1].items. On the second run, you push 2 more categories, so for those you want to be pushing into this.categories[2].items and this.categories[3].items:

Fixed:

        go: function () {
            for (var i=0; i<DATA.length; i++) {
                var items = [];
              var cat = {'num': DATA[i].num, items: items};
                this.push('categories', cat);                     
                for (var j=0; j<DATA[i].items.length; j++) {
                    // push onto the MOST RECENTLY ADDED category's items
                    this.push('categories.' + (this.categories.length-1) + '.items', DATA[i].items[j]);
                }

            }
        }

This works as expected:

http://jsbin.com/jicoxe/2/edit

@andreleblanc-wf

@kevinpschaaf this does NOT resolve the issue. if you look at your 'working as expected' version, you'll still see a duplicate entry at the end of each category. THAT is the issue I'm reporting. yes, clicking the button twice does have unexpected results, but that is not relevant. don't click it twice, the bug is present after 1 click.

it's caused by pushing onto a nested array, and is still evident in your version. please re-open this ticket.

@kevinpschaaf
Owner

Got it-- sorry that was obscured by the other issue.

@kevinpschaaf kevinpschaaf reopened this
@kevinpschaaf
Owner

Confirmed the problem, will land a fix for this soon. Thanks guys!

@andreleblanc-wf

Thanks for the quick turn around, I am both the guys. :D

@kevinpschaaf
Owner

lol, I hadn't noticed the similarity in user names... :)

@kevinpschaaf kevinpschaaf added the p1 label
@kevinpschaaf kevinpschaaf self-assigned this
@kevinpschaaf
Owner

Issue will be resolved by #1795

Confirmed it fixes the reported issue here:
http://jsbin.com/jicoxe/4/edit

@sorvell sorvell closed this in f76ff64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.