次のカスタム バインディングは、もともと別のユーザー RPNiemeyer によって投稿されたものですが、現時点では回答を取得できません。とにかく、次のように見つけることができます:
カスタム バインディングは「checkedWithInit」と呼ばれ、ここでの HTML 要素の checked 属性は、わかりやすくするために便利に true に設定されています。
<input id="yourId" type="checkbox" value="yourValue" data-bind="checkedWithInit: yourInitialStateVariable" checked="true" />
カスタム バインディング コードは、要素の「init」イベント ハンドラの「オーバーライド」です。
ko.bindingHandlers.checkedWithInit = {
init: function(element, valueAccessor, allBindingsAccessor, context) {
var value = valueAccessor();
if (element.checked) {
//if it is an array then push this value to it
if (value.push) {
value.push(element.value);
} else { //otherwise, just write the value
if (ko.isWriteableObservable(value)) {
value(element.checked);
}
}
}
//run the real checked binding's init function
ko.bindingHandlers.checked.init(element, valueAccessor, allBindingsAccessor, context);
},
//always run the checked bindings update function
update: ko.bindingHandlers.checked.update
};
これが役に立てば幸いです。
バレリオ