Skip to content

Commit

Permalink
Ensure that the trackArrayChanges extender only modifies the target o…
Browse files Browse the repository at this point in the history
…bservable once
  • Loading branch information
mbest committed Aug 23, 2013
1 parent 855dc33 commit a6b4942
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/observableArrayChangeTrackingBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ describe('Observable Array change tracking', function() {
{ status : 'deleted', value : 'Gamma', index : 2 },
{ status : 'deleted', value : 'Delta', index : 3 }
]);

// Check that extending the observable again doesn't break anything an only one diff is generated
var changelist2, callCount = 0;
myArray = myArray.extend({trackArrayChanges:true});

myArray.subscribe(function(changes) {
callCount++;
changelist2 = changes;
}, null, 'arrayChange');

myArray(['Gamma']);
expect(callCount).toEqual(1);
expect(changelist2).toEqual([
{ status : 'added', value : 'Gamma', index : 0 }
]);
expect(changelist2).toBe(changelist);
});

it('Should support tracking of a computed observable using extender', function() {
Expand Down
4 changes: 4 additions & 0 deletions src/subscribables/observableArray.changeTracking.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
var arrayChangeEventName = 'arrayChange';
ko.extenders['trackArrayChanges'] = function(target) {
// Only modify the target observable once
if (target.cacheDiffForKnownOperation) {
return;
}
var trackingChanges = false,
cachedDiff = null,
underlyingSubscribeFunction = target.subscribe;
Expand Down

0 comments on commit a6b4942

Please sign in to comment.