Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnockOut run function on value changed

Got simple viewModel:

function viewModel() {
    enabled: ko.observable(false);
    ...
}

and some binding like:

<input data-bind="hasFocus: enabled" />

and i want run some function on focus, and other on focus lost( or enabled = true/false) but run when value changes. Any help?

like image 969
Roar Avatar asked Aug 15 '13 13:08

Roar


People also ask

How do I assign a value to knockout observable?

To create an observable, assign the ko. observable function to the variable. A default value can be specified in the constructor of the call. Knockout then converts your variable into a function and tracks when the value changes, in order to notify the UI elements associated with the variable.

What is applyBindings in knockout?

applyBindings do, The first parameter says what view model object you want to use with the declarative bindings it activates. Optionally, you can pass a second parameter to define which part of the document you want to search for data-bind attributes. For example, ko.

What is the function of knockout?

A knockout, as related to genomics, refers to the use of genetic engineering to inactivate or remove one or more specific genes from an organism. Scientists create knockout organisms to study the impact of removing a gene from an organism, which often allows them to then learn something about that gene's function.

What does Ko unwrap do?

unwrapObservable(item) Looking at the code, that call basically checks to see if item is an observable. If it is, return the value(), if it's not, just return the value.


1 Answers

You could subscribe to your enabled function, such as:

enabled.subscribe(function(newValue) {
   if(newValue) {  // Has focus

   } else { 
       // No focus
   }
});
like image 59
Chris Dixon Avatar answered Oct 10 '22 09:10

Chris Dixon