user3796786
user3796786

Reputation: 209

knockoutjs: multiple valueUpdates and choose which update happend

I have an input field with a value binding. The binding references a writable computed observable.

The intended behavior is:

However, I only can choose updateValue: 'input' or updateValue: 'keydown', but not both. In the JavaScript code, the writable computed is not able to check which event occurred.

I need this behavior for this reason: if the user is just typing I want to fill an auto-suggestion box and want to bold the already typed letters. For this I need the keydown event. But if the user finishes typing (losing focus) I want to do some verification and then save the entered value. If I use input the verification is working but the auto-suggestion feature is broken. If I use keydown the user isn't able to type something, because the verification always fails and the user cannot finish typing.

Upvotes: 0

Views: 92

Answers (1)

Rahul
Rahul

Reputation: 5774

Suppose you have computed named self.inputValue which references to the input field.

You can apply rateLimit extender on perticular computed..

e.g.

self.inputValue = ko.computed(function(){
    //perform the required logic
}).extend({ rateLimit: { method: "notifyWhenChangesStop", timeout: 400 } });;

where timeout is in miliseconds. It will hold the updation or notification of value for 400 miliseconds.

This should solve your problem. A small JSFiddle I've made for you : http://jsfiddle.net/rahulrulez/x8jmcpLh/

rateLimit was introduced in KO 3.1.0, if you are using old library then use throttle instead.

Upvotes: 1

Related Questions