user1405195
user1405195

Reputation: 1681

Pass an extra argument to paging extender

I need to amend this paging extender: http://jsfiddle.net/zeelux/B4vN2/ to accept a currentPage variable rather than it be hard-coded.

pageSize is passed as an argument but I'm not familiar with the syntax how to add an additional argument.

e.g.

ko.extenders.paging = function(target, pageSize, currentPage) {
...
}

and

var ViewModel = function() {
    this.items = ko.observableArray([...]).extend({
        paging: 5, 2
    });
};

isn't working.

Upvotes: 1

Views: 156

Answers (1)

nemesv
nemesv

Reputation: 139788

You cannot add multiple arguments to your extender function but you can pass in an object as the second argument of the extender, which contains the two config options:

ko.extenders.paging = function(target, config) {
    // default pageSize to 10
    var _pageSize = ko.observable(config.pageSize || 10),
    // default current page to 1
    _currentPage = ko.observable(config.currentPage || 1); 
   //...
}

Then you can pass in an object with the right properties when using the extender:

var ViewModel = function() {
    this.items = ko.observableArray([...]).extend({
         paging: { pageSize: 5, currentPage : 2 }
    });
};

Demo JSFiddle.

Note: you can make the extender even smarter: based on the config parameter's type if it is an number use it as the pagesize etc.

Upvotes: 2

Related Questions