I have a hidden form that is interfaced with a more graphic/user-friendly interface, and one of the hidden inputs is being “watched” with the following function:
$('#mag-pg input')[0].watch('value', function(prop, oldval, newval){
if(printRef instanceof Object){
var curSz = $('#mag-sz input').val();
toggleBtn($('#mag-sz button'), 'inactive');
for(u in printRef[newval]){
toggleBtn($('#mag-sz button[value='+u+"]"), 'active');
}
$('#mag-sz button[value='+curSz+']').click();
}
updateOrderInfo();
console.log(newval);
return newval;
});
The watch function is as follows:
if (!Object.prototype.watch) {
Object.defineProperty(Object.prototype, "watch", {
enumerable: false
, configurable: true
, writable: false
, value: function (prop, handler) {
var
oldval = this[prop]
, newval = oldval
, getter = function () {
return newval;
}
, setter = function (val) {
oldval = newval;
return newval = handler.call(this, prop, oldval, val);
}
;
if (delete this[prop]) { // can't watch constants
Object.defineProperty(this, prop, {
get: getter
, set: setter
, enumerable: true
, configurable: true
});
}
}
});
}
This works very well, up until I try to submit the hidden form. The problem is that Chrome does not submit the value of the hidden input that is being watched. This is very odd because checking the value of the input using the console returns the correct value. I have checked the HTTP request using fiddler and the value of the input is definitely not being sent to the server. It would thus appear that Chrome is not properly applying the new value to the input, even though the console would suggest otherwise. I am wondering if anyone has seen this before or if there something I am doing wrong with the watch.
I don’t want to remove the watch, because the point of it is to ensure synchronization. This application is complex and without the value being watched, it would be very easy for different parts of the app to not be updated when they should.
Every other browser correctly submits the input value to the server.
That watch implementation is kind of….incomplete. Maybe this will work better? https://gist.github.com/3196198