In Twitter Bootstrap for a checkbox with labels is used markup like:
<label class="checkbox">
<input type="checkbox" value="option1">
Option 1
</label>
Knockout standard 'text' binding for label is not working: checkbox markup simple replaces with text. I have created a custom binding:
ko.bindingHandlers['checkboxLabelText'] = {
'update': function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if ((value === null) || (value === undefined))
value = "";
$(element).html($(element).html() + " " + _.escape(value));
}
};
It works fine with inline templates,
<!-- This code works -->
<div class="accordion-inner">
<!-- ko foreach:categories -->
<div class="control-group">
<div class="controls">
<label class="checkbox" data-bind="checkboxLabelText: title">
<input type="checkbox" data-bind="checked: checked" />
</label>
</div>
</div>
<!-- /ko -->
</div>
but not works in named templates: checkbox appears but does not have bindings.
<!-- This code does not work -->
<div class="accordion-inner" data-bind="template: {foreach: categories, name: 'checkboxEditorTemplate'}">
</div>
<script type="text/html" id="checkboxEditorTemplate">
<div class="control-group">
<div class="controls">
<label class="checkbox" data-bind="checkboxLabelText: title">
<input type="checkbox" data-bind="checked: $data.checked()" />
</label>
</div>
</div>
</script>
Any help?