I'm having issues with the checked binding in a checkbox list.
JS
function vm() {
var self = this;
self.categories = [
{id: 1, name: "Category 1"},
{id: 2, name: "Category 2"},
{id: 3, name: "Category 3"}
];
// Assume this item came from server side,
// that's why i'm using the mapping plugin here.
var rawItem = { links: [1] };
self.item = ko.mapping.fromJS(rawItem);
}
ko.applyBindings(new vm());
HTML
<div data-bind="foreach: categories">
<div>
<label>
<span data-bind="text: name"></span>
<input class="checkbox" type="checkbox" data-bind="checked: $root.item.links, attr: {value: id}">
</label>
</div>
</div>
<div data-bind="text: ko.toJSON(item.links)"></div>
As you can see in the fiddle, first issue is that the "id" property from the self.category
items is somehow casted to a string, and that is causing the comparison fail. Anyway, casting the item links
to string don't work as expected too.
In short, goal is: check fields on page load according to values from the categories
array.
Since i'm reproducing this from a more complex scenario, I've added the mapping plugin to put in any possible factor.