I would recommend a wrapper that will handle the changed value. For example, you can have a JavaScript function, for example:
function Variable(initVal, onChange) { this.val = initVal; //Value to be stored in this object this.onChange = onChange; //OnChange handler //This method returns stored value this.GetValue = function() { return this.val; } //This method changes the value and calls the given handler this.SetValue = function(value) { this.val = value; this.onChange(); } }
And then you can make an object out of it that will contain the value that you want to control, as well as a function that will be called when the value changes. For example, if you want to receive a warning when a value changes, and the initial value is 10, you should write the code as follows:
var myVar = new Variable(10, function(){alert("Value changed!");});
The function(){alert("Value changed!");} handler function(){alert("Value changed!");} Will be called (if you look at the code) when SetValue() called.
You can get the value like this:
alert(myVar.GetValue());
You can set the value as follows:
myVar.SetValue(12);
And immediately after that, a warning appears on the screen. See how it works: http://jsfiddle.net/cDJsB/
Cipi 03 Sep '10 at 13:24 2010-09-03 13:24
source share