From 64f69a9b050559e5dfea0767540d0c0f8eb2dc54 Mon Sep 17 00:00:00 2001 From: Joscha Rohmann Date: Sun, 14 Feb 2016 00:04:32 +0100 Subject: [PATCH] Fixed some bugs in the sort-extender. Closes #132. Also fixed a bug in array-observable.move(). --- src/query/ExtenderHelper.js | 14 ++++++++++++-- src/query/observable.js | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/query/ExtenderHelper.js b/src/query/ExtenderHelper.js index d29aa8a..b553d70 100644 --- a/src/query/ExtenderHelper.js +++ b/src/query/ExtenderHelper.js @@ -130,7 +130,15 @@ define([ } else if (operation.type == 'sort') { if (blocks.isString(operation.sort)) { collection = blocks.clone(collection).sort(function (valueA, valueB) { - return valueA[operation.sort] - valueB[operation.sort]; + valueA = blocks.unwrap(valueA[operation.sort]); + valueB = blocks.unwrap(valueB[operation.sort]); + if (valueA > valueB) { + return 1; + } + if (valueA < valueB) { + return -1; + } + return 0; }); } else if (blocks.isFunction(operation.sort)) { collection = blocks.clone(collection).sort(operation.sort.bind(observable.__context__)); @@ -204,11 +212,13 @@ define([ break; case EXISTS: newConnections[index] = viewIndex; + if (view.__value__.indexOf(collection[index]) != index) { + view.move(view.__value__.indexOf(collection[index]), index); + } viewIndex++; break; } }); - view._connections = newConnections; view.update = update; view.update(); diff --git a/src/query/observable.js b/src/query/observable.js index 63617ba..4c964d7 100644 --- a/src/query/observable.js +++ b/src/query/observable.js @@ -517,7 +517,7 @@ define([ } for (var i = 0; i < elements.length; i++) { - element = elements[i].element; + element = elements[i].element = elements[i].element || ElementsData.byId(elements[i].elementId).dom; chunkManager.insertAt(element, targetIndex, chunkManager.getAt(element, sourceIndex)); }