【发布时间】:2013-08-02 23:01:57
【问题描述】:
我的页面上有一个使用值绑定具有可观察绑定的输入。但是,如果新值小于指定的字符数,我有一个扩展器会阻止对可观察对象的写入:
ko.extenders.minLengthRevert = function(target, minLength) {
"use strict";
//create a write able computed observable to intercept writes to our observable
var result = ko.computed({
read: target, //always return the original observables value
write: function(newValue) {
var current = target();
if (current !== newValue){
if (newValue.length >= minLength){
target(newValue);
}
}
}
});
//return the new computed observable
result(target());
return result;
};
这很好用,除非我清除输入框的值,否则该框不会恢复为旧值(但 observable 不会正确更新)。所以基本上我需要一些方法来强制输入框从 observable 更新,即使 observable 没有更新。这是一个演示行为的 JSFiddle:http://jsfiddle.net/4Z5bp/
【问题讨论】:
标签: knockout.js