Skip to content

Commit

Permalink
Observable array mutation methods that would normally return the arra…
Browse files Browse the repository at this point in the history
…y now return the observable array.
  • Loading branch information
mbest committed May 8, 2015
1 parent 9b90cb5 commit 0135e3f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions spec/observableArrayBehaviors.js
Expand Up @@ -277,6 +277,14 @@ describe('Observable Array', function() {
expect(timesEvaluated).toEqual(1);
});

it('Should return the observableArray reference from "sort" and "reverse"', function() {
expect(testObservableArray.reverse()).toBe(testObservableArray);
expect(testObservableArray.sort()).toBe(testObservableArray);

// Verify that reverse and sort notified their changes
expect(notifiedValues).toEqual([ [3, 2, 1], [1, 2, 3] ]);
});

it('Should inherit any properties defined on ko.subscribable.fn, ko.observable.fn, or ko.observableArray.fn', function() {
this.after(function() {
delete ko.subscribable.fn.subscribableProp; // Will be able to reach this
Expand Down
3 changes: 2 additions & 1 deletion src/subscribables/observableArray.js
Expand Up @@ -101,7 +101,8 @@ ko.utils.arrayForEach(["pop", "push", "reverse", "shift", "sort", "splice", "uns
this.cacheDiffForKnownOperation(underlyingArray, methodName, arguments);
var methodCallResult = underlyingArray[methodName].apply(underlyingArray, arguments);
this.valueHasMutated();
return methodCallResult;
// The native sort and reverse methods return a reference to the array, but it makes more sense to return the observable array instead.
return methodCallResult === underlyingArray ? this : methodCallResult;
};
});

Expand Down

0 comments on commit 0135e3f

Please sign in to comment.