Skip to content
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

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

Closed
andrepl opened this issue Jun 4, 2015 · 7 comments
Assignees
Labels

Comments

@andrepl
Copy link

andrepl commented Jun 4, 2015

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 nested dom-repeat with sort at attribute shows duplicate entries when adding new items. nested dom-repeat with sort attribute shows duplicate entries when adding new items. Jun 4, 2015
@kevinpschaaf
Copy link
Member

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
Copy link

@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
Copy link
Member

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

@kevinpschaaf kevinpschaaf reopened this Jun 4, 2015
@kevinpschaaf
Copy link
Member

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

@andreleblanc-wf
Copy link

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

@kevinpschaaf
Copy link
Member

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

@kevinpschaaf kevinpschaaf added the p1 label Jun 7, 2015
@kevinpschaaf kevinpschaaf self-assigned this Jun 7, 2015
@kevinpschaaf
Copy link
Member

Issue will be resolved by #1795

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants