Skip to content

Commit e6a6970

Browse files
marcovdbErikSchierboom
authored andcommitted
Unwrap value in isObservableArray (#17)
* Unwrap value in isObservableArray This will allow the extender to work on a computed observable as well. * Added tests for computed observables * Improved array detection
1 parent 162882f commit e6a6970

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

knockout-paging.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@
2121
// --------
2222
"use strict";
2323

24+
// polyfill for IE < 9
25+
if (!Array.isArray) {
26+
Array.isArray = function(arg) {
27+
return Object.prototype.toString.call(arg) === '[object Array]';
28+
};
29+
}
30+
2431
// Utilities
2532
function isObservableArray(value) {
26-
return ko.isObservable(value) && 'push' in value;
33+
return ko.isObservable(value) && Array.isArray(ko.unwrap(value));
2734
}
2835

2936
function createRange(min, max) {
@@ -234,4 +241,4 @@
234241
ko.pagedObservableArray = function (initialValue, options) {
235242
return ko.observableArray(initialValue).extend({ paged: options });
236243
};
237-
}));
244+
}));

test/spec.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ describe("paged extender", function () {
2424
emptyObservableArray,
2525
singlePageObservableArray,
2626
smallNumberPagesObservableArray,
27-
largeNumberPagesObservableArray;
27+
largeNumberPagesObservableArray,
28+
smallNumberPagesComputedArray;
2829
spy = sinon.spy();
2930

3031
beforeEach(function () {
@@ -41,15 +42,45 @@ describe("paged extender", function () {
4142
singlePageObservableArray = ko.observableArray([1]).extend({ paged: options });
4243
smallNumberPagesObservableArray = ko.observableArray(createRange(1, 7)).extend({ paged: options });
4344
largeNumberPagesObservableArray = ko.observableArray(createRange(1, 30)).extend({ paged: options });
45+
smallNumberPagesComputedArray = ko.pureComputed(function () { return createRange(1, 7) }).extend( { paged: options });
4446
});
4547

4648
context("on regular observable", function () {
4749
it("throws", function () {
48-
var regularObservable = ko.observable();
50+
var regularObservable = ko.observable('foo');
4951
expect(regularObservable.extend.bind(regularObservable, { paged: {} })).to.throw(Error);
5052
});
5153
});
5254

55+
context("on computed observable that does not return an array", function () {
56+
it("throws", function () {
57+
var computedObservable = ko.pureComputed(function() {
58+
return 'foo';
59+
});
60+
expect(computedObservable.extend.bind(computedObservable, { paged: {} })).to.throw(Error);
61+
});
62+
});
63+
64+
context("on computed observable that returns an array", function () {
65+
var computedObservable = ko.pureComputed(function() {
66+
return createRange(1, 7);
67+
});
68+
it("does not throw", function () {
69+
expect(computedObservable.extend.bind(computedObservable, { paged: {} })).to.not.throw(Error);
70+
});
71+
context("on multi-page paged computed array", function () {
72+
it("itemCount is the same as for multi-page paged observable array", function() {
73+
expect(smallNumberPagesComputedArray.itemCount()).to.equal(smallNumberPagesObservableArray.itemCount());
74+
});
75+
it("pageItems is the same as for multi-page paged observable array", function() {
76+
expect(smallNumberPagesComputedArray.pageItems()).to.deep.equal(smallNumberPagesObservableArray.pageItems());
77+
});
78+
it("pages is the same as for multi-page paged observable array", function() {
79+
expect(smallNumberPagesComputedArray.pages()).to.deep.equal(smallNumberPagesObservableArray.pages());
80+
});
81+
});
82+
});
83+
5384
context("on empty paged observable array", function () {
5485
it("itemCount is 0", function () {
5586
expect(emptyObservableArray.itemCount()).to.equal(0);

0 commit comments

Comments
 (0)