Reputation: 859
I use the next code for watch changes in javascript object: https://gist.github.com/eligrey/384583
I have a next code for check updates:
var Obj = {
prop: 0,
inc: function() {
this.prop ++;
}
};
Obj.watch('prop', function(prop, oldv, newv) {
console.log(prop);
console.log(oldv);
console.log(newv);
});
// on button click i increment prop
:
$("#btn").on('click', function() {
Obj.inc();
});
When i run this code, and click button i get in console:
prop
0
1
It's correctly.
But, when i click the next time, i get:
z
undefined
NaN
Online demo: http://jsbin.com/jenarapufo/2/edit?html,js,console,output
Why?
Upvotes: 0
Views: 182
Reputation: 17735
You'll need to return the new value from the callback:
Obj.watch('prop', function(prop, oldv, newv) {
console.log(prop);
console.log(oldv);
console.log(newv);
return newv;
});
Upvotes: 1
Reputation: 2921
You need to return new value from watch
callback. Update Your handler:
Obj.watch('prop', function(prop, oldv, newv) {
...
return newv;
})
Upvotes: 2