How can I handle a subscribe that would trigger after the dom is updated? I am trying to run some jQuery that updates the even visible rows on a table by giving them a css class of "even". The subscribe function is listening for a change in a property carsOnly
, which toggles whether or not just car items should be shown, or if all items should be shown.
Issue: in my subscribe, the jQuery that sets the even rows' CSS gets run before the dom is updated, so it adds "even" class to rows that are then hidden a split second later when the dom does finally update.
Html:
<input type="checkbox" data-bind="checked: carsOnly" /><label>Show cars only</label>
<tbody data-bind="foreach: items">
<tr data-bind="visible: !viewModel.carsOnly() || isCar()">
<td data-bind="text: vehicleType"></td> <!-- would display 'car' or 'truck' -->
</tr>
</tbody>
Js:
var viewModel = ko.mapping.fromJS({
carsOnly: true,
items: [{vehicleType:'car'},{vehicleType:'car'},{vehicleType:'truck'}]
});
viewModel.carsOnly.subscribe(function(newVal) {
// problem is, this fires off _before_ any of the rows in the table are made invisible
$("tbody tr").removeClass("even");
$("tbody tr:visible:even").addClass("even");
});
ko.applyBindings(viewModel);
Css:
tbody tr.even { background-color: blue; }