Skip to content
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
11 changes: 9 additions & 2 deletions knockout-paging.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@
// --------
"use strict";

// polyfill for IE < 9
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}

// Utilities
function isObservableArray(value) {
return ko.isObservable(value) && 'push' in value;
return ko.isObservable(value) && Array.isArray(ko.unwrap(value));
}

function createRange(min, max) {
Expand Down Expand Up @@ -234,4 +241,4 @@
ko.pagedObservableArray = function (initialValue, options) {
return ko.observableArray(initialValue).extend({ paged: options });
};
}));
}));
35 changes: 33 additions & 2 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe("paged extender", function () {
emptyObservableArray,
singlePageObservableArray,
smallNumberPagesObservableArray,
largeNumberPagesObservableArray;
largeNumberPagesObservableArray,
smallNumberPagesComputedArray;
spy = sinon.spy();

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

context("on regular observable", function () {
it("throws", function () {
var regularObservable = ko.observable();
var regularObservable = ko.observable('foo');
expect(regularObservable.extend.bind(regularObservable, { paged: {} })).to.throw(Error);
});
});

context("on computed observable that does not return an array", function () {
it("throws", function () {
var computedObservable = ko.pureComputed(function() {
return 'foo';
});
expect(computedObservable.extend.bind(computedObservable, { paged: {} })).to.throw(Error);
});
});

context("on computed observable that returns an array", function () {
var computedObservable = ko.pureComputed(function() {
return createRange(1, 7);
});
it("does not throw", function () {
expect(computedObservable.extend.bind(computedObservable, { paged: {} })).to.not.throw(Error);
});
context("on multi-page paged computed array", function () {
it("itemCount is the same as for multi-page paged observable array", function() {
expect(smallNumberPagesComputedArray.itemCount()).to.equal(smallNumberPagesObservableArray.itemCount());
});
it("pageItems is the same as for multi-page paged observable array", function() {
expect(smallNumberPagesComputedArray.pageItems()).to.deep.equal(smallNumberPagesObservableArray.pageItems());
});
it("pages is the same as for multi-page paged observable array", function() {
expect(smallNumberPagesComputedArray.pages()).to.deep.equal(smallNumberPagesObservableArray.pages());
});
});
});

context("on empty paged observable array", function () {
it("itemCount is 0", function () {
expect(emptyObservableArray.itemCount()).to.equal(0);
Expand Down