Dynamically accessing properties of knockoutjs obs

2019-07-19 18:19发布

I am using the below code for handling sort functionality. It is working for me. But is there any way to make the code as common and so i can use it whenever needed.

<span class="sorting" data-bind="click: function(){ ui.items.sort(function(a,b){ return a.Username < b.Username ? -1 : 1; }); }">User Name</span>

<span class="sorting" data-bind="click: function(){ ui.items.sort(function(a,b){ return a.Firstname < b.Firstname ? -1 : 1; }); }">
            First Name</span>

<span class="sorting" data-bind="click: function(){ ui.items.sort(function(a,b){ return a.Lastname < b.Lastname ? -1 : 1; }); }">
            Last Name</span>

scripts

ui = new ListUI(config);

ko.applyBindings(ui);


var ListUI = function ListUIF(config) {
   this.items = ko.observableArray([]);
}

var item = function itemF(data) {

        var self = this;

        self.Username = ko.observable(data.Username);
        self.Firstname = ko.observable(data.Firstname);
        self.Lastname = ko.observable(data.Lastname);
}

The code above is working fine, but i didn't want the sorting code to be repeated.

<span class="sorting" data-bind="click: function(){ ui.items.sort(function(a,b){   return a.Lastname < b.Lastname ? -1 : 1; }); }">
            Last Name</span>

Instead i want something like this

<span class="sorting" data-bind="click: sortFunction">
            Last Name</span>

var sortFunction = function sortFunctionF(a, b){

   return a.Username < b.Username : -1 : 1;  //How to make this common for other property also like Firstname, Lastname etc.

 }

1条回答
成全新的幸福
2楼-- · 2019-07-19 18:43

There's basically two options: Have three separate functions sortByUsername, sortByFirstname and sortByLastname, or you could do a custom binding that takes in additional information and sets up the sort.

The second one could look something like this:

<span class="sorting" data-bind="sortFunction: 'Username'">User Name</span>

<span class="sorting" data-bind="sortFunction: 'Firstname'">
        First Name</span>

<span class="sorting" data-bind="sortFunction: 'Lastname'}">
        Last Name</span>

And then the custom binding for sortFunction:

ko.bindingHandlers.sortFunction = {
    update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        var sortBy = ko.utils.unwrapObservable(valueAccessor());
        var items = viewModel.items;
        $(element).unbind('click.sort').bind('click.sort', function() {
            items.sort(function(a, b) {
                return a[sortBy]() < b[sortBy]() ? -1 : 1;
             });
        });
    }
}

Fiddle

Another option as mentioned by Joeseph would be to pass the property name into the click function, which would then return a function. I don't think this is as nice an option as the custom binding, but Here's a fiddle that does that:

<span class="sorting" data-bind="click: getSortFunction('Username')">User Name</span>

<span class="sorting" data-bind="click: getSortFunction('Firstname')">
        First Name</span>

<span class="sorting" data-bind="click: getSortFunction('Lastname')}">
        Last Name</span>

And then your viewmodel would change to add the function:

var ListUI = function ListUIF(items) {
    var self = this;
   self.items = ko.observableArray(items);

    self.getSortFunction = function(prop) {
        return function() {
            self.items.sort(function(a, b) {
                return a[prop]() < b[prop]() ? -1 : 1;
            });
        };
    };

    return self;
}
查看更多
登录 后发表回答