how to reapply knockout binding

Posted by MikeW on Stack Overflow See other posts from Stack Overflow or by MikeW
Published on 2012-07-11T03:27:06Z Indexed on 2012/07/11 15:15 UTC
Read the original article Hit count: 287

Filed under:
|

Currently I have a knockout binding that stripes rows in a list which works fine

ko.bindingHandlers.stripe = {
    update: function (element, valueAccessor, allBindingsAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor()); //creates the dependency
        var allBindings = allBindingsAccessor();
        var even = allBindings.evenClass;
        var odd = allBindings.oddClass;

        //update odd rows
        $(element).children(":nth-child(odd)").addClass(odd).removeClass(even);
        //update even rows
        $(element).children(":nth-child(even)").addClass(even).removeClass(odd); ;
    }
}

Triggered from

<button data-bind="click: addWidget" style="display:none">Add Item</button>

The problem I have is when reloading data from the server , I call addWidget() manually in the view model the stripe binding handler is not applied - all rows appear as same color, if I click the html button then the binding happens and stripes appear

var ViewModel = function() {

     self.addWidget();

});

Is it possible to reapply this custom binding manually in js?

Thanks

Edit:

The stripe binding gets applied like so

<div data-bind="foreach: widgets, stripe: widgets, evenClass: 'light', oddClass: 'dark'">

© Stack Overflow or respective owner

Related posts about knockout.js

Related posts about zebra-striping