最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setting a call back function for observable objects - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Sep 19, 2012 at 6:54 DhananjayaDhananjaya 1,6152 gold badges16 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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());
发布评论

评论列表(0)

  1. 暂无评论