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

Fix trackable beforeId bug. #208

Merged
merged 1 commit into from
Jun 6, 2017
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
26 changes: 12 additions & 14 deletions Trackable.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ define([
revision++;
var target = event.target;
event = lang.delegate(event, defaultEventProps[type]);
var beforeId = event.beforeId;

when(observed._results || observed._partialResults, function (resultsArray) {
/* jshint maxcomplexity: 32 */
Expand All @@ -245,16 +246,13 @@ define([
}

var i, j, l, ranges = observed._ranges, range;
/*if(++queryRevision != revision){
throw new Error('Query is out of date, you must observe() the' +
' query prior to any data modifications');
}*/

var targetId = 'id' in event ? event.id : store.getIdentity(target);
var removedFrom = -1,
removalRangeIndex = -1,
insertedInto = -1,
insertionRangeIndex = -1;
insertionRangeIndex = -1,
removedBeforeId;
if (type === 'delete' || type === 'update') {
// remove the old one
for (i = 0; removedFrom === -1 && i < ranges.length; ++i) {
Expand All @@ -267,6 +265,7 @@ define([
if (store.getIdentity(object) == targetId) {
removedFrom = event.previousIndex = j;
removalRangeIndex = i;
removedBeforeId = beforeId == targetId;
resultsArray.splice(removedFrom, 1);

range.count--;
Expand Down Expand Up @@ -298,10 +297,10 @@ define([

sampleArray = resultsArray.slice(range.start, range.start + range.count);

if ('beforeId' in event) {
candidateIndex = event.beforeId === null
if (beforeId !== undefined) {
candidateIndex = beforeId === null
? sampleArray.length
: findObject(sampleArray, event.beforeId);
: findObject(sampleArray, beforeId);
}

if (candidateIndex === -1) {
Expand Down Expand Up @@ -338,10 +337,9 @@ define([
// about where it was inserted or moved to. If it is an update, we leave
// its position alone. otherwise, we at least indicate a new object

var range,
possibleRangeIndex = -1;
if ('beforeId' in event) {
if (event.beforeId === null) {
var possibleRangeIndex = -1;
if (beforeId !== undefined && !removedBeforeId) {
if (beforeId === null) {
insertedInto = resultsArray.length;
possibleRangeIndex = ranges.length - 1;
} else {
Expand All @@ -350,7 +348,7 @@ define([

insertedInto = findObject(
resultsArray,
event.beforeId,
beforeId,
range.start,
range.start + range.count
);
Expand Down Expand Up @@ -401,7 +399,7 @@ define([
event.beforeIndex = ranges[begin].start;
for (i = begin; i < ranges.length; ++i) {
ranges[i].start++;
}
}
}
}
// update the total
Expand Down
12 changes: 12 additions & 0 deletions tests/Trackable.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,18 @@ define([
assert.strictEqual(data[data.length - 1].id, 3);
},

'updated item - with options.beforeId and no queryExecutor - update beforeId item': function () {
var store = createPrimeNumberStore(),
collection = store.track();
var data = collection._results;
store.put(store.getSync(4), { beforeId: 4 });

var len = data.length;
for (var i = 0; i < len; i++) {
assert.strictEqual(data[i].id, i, 'Item ' + i + ' is not correct.d');
}
},

'type': function () {
assert.isFalse(store === store.track(function () {}));
},
Expand Down