Knockout-2.2.0, subscribe get value before change AND new value

后端 未结 3 1521
我在风中等你
我在风中等你 2021-02-12 16:59

jsfiddle link: http://jsfiddle.net/T8ee7/

When I call Knockout\'s subscribe method is there a way I can get both the previous and new value? Right now, I can only call g

3条回答
  •  失恋的感觉
    2021-02-12 17:15

    This seems to work for me

    ko.observable.fn.beforeAndAfterSubscribe = function (callback, target) {
     var _oldValue;
     this.subscribe(function (oldValue) {
         _oldValue = oldValue;
     }, null, 'beforeChange');
       this.subscribe(function (newValue) {
         callback.call(target, _oldValue, newValue);
     });
    }; 
    
    • See more at: http://ideone.com/NPpNcB#sthash.wJn57567.dpuf

提交回复
热议问题