If, for whatever reason, you still want to use containerless control flow without putting comment markup in your code, I've written a binding handler that will do it for you.
Apply it like so:
<div data-bind="wrap: {foreach: arr}" data-bind-inner="text: $data"></div>
with the binding for the container included as the data-bind
attribute and the binding for the element itself as the data-bind-inner
attribute. The handler actually creates comment tags and applies the specified binding to them, so there's nothing really tricky going on.
ko.bindingHandlers.wrap = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var outerBinding = valueAccessor(),
vNodeOpen = document.createComment('ko'),
vNodeClose = document.createComment('/ko');
element.dataset.bind = element.dataset.bindInner;
// Enclose element in comment nodes
element.parentNode.insertBefore(vNodeClose, element);
element.parentNode.insertBefore(element, vNodeClose);
element.parentNode.insertBefore(vNodeOpen, element);
ko.applyBindingsToNode(vNodeOpen, outerBinding, bindingContext);
}
};
Demo fiddle
As written, it applies to a single node (with its descendant nodes, of course); you can't wrap a pair of siblings, but it could be extended to take a siblingCount
option.