Another solution to this problem is to create your own binding. It sounds complicated, but it is very simple, and KO was designed with custom bindings in mind. I want the base package to have more of them, but they are trivial to create. The advantage of this solution is that your binding is simple and straightforward. Here is an example called hidden:
ko.bindingHandlers.hidden = (function() { function setVisibility(element, valueAccessor) { var hidden = ko.unwrap(valueAccessor()); $(element).css('visibility', hidden ? 'hidden' : 'visible'); } return { init: setVisibility, update: setVisibility }; })();
And used in your HTML as:
data-bind="hidden: !repeat()"
source share