Skip to content

Commit

Permalink
Merge pull request #2314 from Polymer/fix-dom-repeat
Browse files Browse the repository at this point in the history
Use numeric sort when removing dom-repeat instances
  • Loading branch information
Steve Orvell committed Aug 20, 2015
2 parents 711039a + c967583 commit 0f8483d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/lib/template/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@
return this.collection.getKey(a) - this.collection.getKey(b);
},

_numericSort: function(a, b) {
return a - b;
},

// Render method 2: incremental update using splices with user sort applied
// ----
// Removed/added keys are deduped, all removed rows are detached and pooled
Expand Down Expand Up @@ -438,7 +442,8 @@
if (removedIdxs.length) {
// Sort removed instances idx's then remove backwards,
// so we don't invalidate instance index
removedIdxs.sort();
// use numeric sort, default .sort is alphabetic
removedIdxs.sort(this._numericSort);
for (var i=removedIdxs.length-1; i>=0 ; i--) {
var idx = removedIdxs[i];
// Removed idx may be undefined if item was previously filtered out
Expand Down
26 changes: 25 additions & 1 deletion test/unit/dom-repeat-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,28 @@
}
});
</script>
</dom-module>
</dom-module>

<dom-module id="x-primitive-large">
<template>
<template id="repeater" is="dom-repeat" items="{{items}}">
<div>{{item}}</div>
</template>
</template>
<script>
Polymer({
is: 'x-primitive-large',
properties: {
items: {
value: function() {
var ar = [];
for (var i = 0; i < 11; i++) {
ar.push(i);
}
return ar;
}
}
}
});
</script>
</dom-module>
14 changes: 14 additions & 0 deletions test/unit/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ <h4>inDocumentRepeater</h4>
</template>
</div>

<h4>x-primitive-large</h4>
<x-primitive-large id="primitiveLarge"></x-primitive-large>

<div id="inDocumentContainer">
</div>

Expand Down Expand Up @@ -1267,6 +1270,17 @@ <h4>inDocumentRepeater</h4>
});
});

test('large splice', function(done) {
primitiveLarge.splice('items', 0, 10);
setTimeout(function() {
var stamped =
Polymer.dom(primitiveLarge.root).querySelectorAll('*:not(template)');
assert.equal(stamped.length, 1, 'total stamped count incorrect');
assert.equal(stamped[0].textContent, '10');
done();
});
});

test('css scoping retained when re-ordering', function(done) {
if (!Polymer.Settings.useShadow) {
// Confirm initial scoping
Expand Down

0 comments on commit 0f8483d

Please sign in to comment.