Created
February 8, 2013 15:07
-
-
Save kunev/4739557 to your computer and use it in GitHub Desktop.
Custom throttle extender for knockout.js that throttles input but updates the binding immediately when blurring the text field bound to it. Based on the original throttle extender.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.extenders.smart_throttle = function(target, timeout) { | |
target['throttleEvaluation'] = timeout; | |
target['lastUpdatedValue'] = null; | |
var writeTimeoutInstance = null; | |
return ko.computed({ | |
'read': target, | |
'write': function(value) { | |
if (value == target()) { | |
clearTimeout(writeTimeoutInstance); | |
target(target['lastUpdatedValue']); | |
return; | |
} | |
clearTimeout(writeTimeoutInstance); | |
target['lastUpdatedValue'] = value; | |
writeTimeoutInstance = setTimeout(function() { | |
target(value); | |
}, timeout); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment