A quick thought going with your existing code:
In your extender, you could see if you are dealing with a non-writeable computed observable, then return an observable that has gone through your extender and has subscribed to the computed.
ko.extenders.restrictChangeSpeed = function(target, timeout) {
var writeTimeoutInstance = null;
var currentValue = target();
var updateValueAgain = false;
var interceptor;
if (ko.isComputed(target) && !ko.isWriteableObservable(target)) {
interceptor = ko.observable().extend({ restrictChangeSpeed: timeout });
target.subscribe(interceptor);
return interceptor;
}
....
The key is that you need something to go through your "write" logic. With a normal computed, it would just re-evaluate its "read" logic and update, never going through your "write" code.
Here is a sample: http://jsfiddle.net/rniemeyer/GPbrR/
Also, you had a typo in your fiddle:
self.restrictChangeSpeedComputedVar1 = ko.computed(this.var1).extend({restictChangeSpeed:1000});
You had misspelled your extender name (restict
instead of restrict
), which made me scratch my head while testing the changes that I added, until I noticed it.
The only interesting thing about my changes is that now someone could potentially write to your computed, but it would always update whenever the underlying computed changed and I don't see why you would intentionally try to write to it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…