How to get the value before changing it? Knockout .js

I use mapping utilities to load some JSON megadata. I know how to detect changes with the "update" callback.

But how to compare the old value with the new value? In the "update" callback, I only get access to the new value.

Of course, when I add the update to JSON, I use the mapping utilities again.

I would like to know if the value is increasing or decreasing. How to do it?

+4
source share
1 answer

The update callback for the ko.mapping plugin takes one parameter argument, which is an object containing

  • data
  • parent
  • target

The parent object still retains the old value.

var mapping = { 'prop': update: function(options) { var oldval = options.parent.prop; var newval = options.data; // do something that uses oldval and newval here return newval; } } 
+1
source

Source: https://habr.com/ru/post/1380016/


All Articles