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

Use numeric sort when removing dom-repeat instances #2314

Merged
merged 2 commits into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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