Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting a call back function for observable objects

I'm trying to integrate knockoutJS variables to a Jquery-UI, so to update my UI when a knockout observable changes, I need a way to call a function when observable changes. I want to set my own call back function so if my observable variable changes this call back function need to be called automatically.

like image 775
Dhananjaya Avatar asked Sep 19 '12 06:09

Dhananjaya


People also ask

What are the three callback functions available during subscription of observables?

On the parameter that was given when creating the observable there are three functions available to send data to the subscribers of the observable: “next”: sends any value such as Numbers, Arrays or objects to it's subscribers. “error”: sends a Javascript error or exception. “complete”: does not send any value.

What is callback observable?

A callback function is a function passed into another function as an argument which is invoked inside the callee function(here greeting) to complete or after some kind of actions.

What happens when you subscribe to an observable?

Observables are declarative —that is, you define a function for publishing values, but it is not executed until a consumer subscribes to it. The subscribed consumer then receives notifications until the function completes, or until they unsubscribe.

What is Observer complete () in angular?

The observer's complete() callback specifies the action to take when the observable has completed producing and emitting data. const observer = { complete: () => console. log('You have used up all the vowels.') }; Js.


1 Answers

You can call the subscribe function on a observable, giving it the callback function to be called when the observable changes.

<input data-bind="value: val"/>

var Model = function() {
  var self = this;
  this.val = ko.observable();  
  this.val.subscribe(function () {
        alert(self.val());                
  });
};
ko.applyBindings(new Model());
like image 140
gbs Avatar answered Oct 21 '22 03:10

gbs