我正在尝试为特殊数字格式创建自定义绑定。
ko.bindingHandlers['valueTest'] = {
'init': function (element, valueAccessor, allBindingsAccessor) {
var valueUpdateHandler = function () {
var modelValue = valueAccessor();
var elementValue = ko.selectExtensions.readValue(element);
ko.expressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'value', elementValue);
}
ko.utils.registerEventHandler(element, "change", valueUpdateHandler);
},
'update': function (element, valueAccessor) {
var newValue = ko.utils.unwrapObservable(valueAccessor());
var elementValue = ko.selectExtensions.readValue(element);
if (newValue !== elementValue) {
ko.selectExtensions.writeValue(element, newValue.toFixed(2).replace(".", ","));
}
}
};
也可在小提琴http://jsfiddle.net/PAFTR/11/
但更新失败(萤火虫说:TypeError:ko.expressionRewriting.writeValueToProperty is not a function)。