how to reapply knockout binding
- by MikeW
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'">