我正在嘗試編寫一個綁定處理程序,它簡化了選擇標記與對象的用法。正在重置綁定處理程序的可寫計算observable
ko.bindingHandlers.objectSelect = {
init: function(element, valueAccessor, allBindingsAccessor) {
var interceptor = ko.computed({
read: function() {
return valueAccessor()[allBindingsAccessor().optionsValue]();
},
write: function(value) {
valueAccessor()[allBindingsAccessor().optionsValue](value);
}
});
ko.applyBindingsToNode(element, {
value: interceptor
});
}
};
在這種小提琴http://jsfiddle.net/p8dn2d2n/2/,如果你運行它時,選擇開始清除,即使是使用valueAllowUnset時。
如果您對第二次選擇發表評論,則所有內容均按預期工作。
<select data-bind="
value: current().val.id,
valueAllowUnset: true,
options: options,
optionsValue: 'id',
optionsText: 'name',
optionsCaption: ' '">
</select>
<!-- Problem here -->
<select data-bind="
objectSelect: current().val,
valueAllowUnset: true,
options: options,
optionsValue: 'id',
optionsText: 'name',
optionsCaption: ' '">
</select>
<p><span data-bind="text: current().val.id" /></p>
<div>
<button data-bind="click: click">New</button>
</div>
我在幹什麼?
我剛剛發現,如果objectSelect自帶選項之後,那麼它的工作原理,但我認爲它應該工作無關的標籤的順序,與直接使用「值」。 – 2014-09-02 14:25:18