diff --git a/spec/observableArrayBehaviors.js b/spec/observableArrayBehaviors.js index 3182e2e5f..350eed412 100644 --- a/spec/observableArrayBehaviors.js +++ b/spec/observableArrayBehaviors.js @@ -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 diff --git a/src/subscribables/observableArray.js b/src/subscribables/observableArray.js index 2e51e7581..6afa193af 100644 --- a/src/subscribables/observableArray.js +++ b/src/subscribables/observableArray.js @@ -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; }; });