12

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.

Dhananjaya
  • 1,510
  • 2
  • 14
  • 19

1 Answers1

37

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());
J Wynia
  • 10,464
  • 4
  • 40
  • 38
gbs
  • 3,529
  • 2
  • 20
  • 18