Today, I had some fun trying to figure out how to fix the height of the jQueryUI dialog. The client wanted it to be exactly 500px. Or something that resembled 500px. Anyway, it definitely shouldn't have been from the top to the bottom of the screen. Although I sure set it to 500.
The fun part is that it had to work in IE7-9, but in *quirks* mode. The client won't switch to the standards mode, since the site (made in early 2000s, tables inside tables all the way down) would break apart.
After some debugging, I figured that one line in a certain method would save me. Namely, a fix would require adding a line at the beginning of the "_size
" method. So, I could just leave it like that..
Except That I Couldn't.
Doing so wouldn't just violate the Open/Closed principle, it would offend the shadows of the Fathers of SOLIDity and the Alt.Net deities.
After all, JavaScript is a dynamic language, right? So, we can do whatever dirty trick we can think of, including messing with "private
" methods.
Extending a jQueryUI Widget by Rewriting a Widget's Private Method? Nothing Could Be Easier!
var proto = $.ui.dialog.prototype;
var _size = proto._size;
proto._size = function(){
this.element.hide();
_size.apply(this);
};
While I could easily put there something like alert('OHMYGOSH!!!')
(and have fun imagining my coworkers trying to figure out what's going on), what I'm actually doing here is just add something to the beginning. So, first I'm saving a reference to the existing function, then I redefine it, adding the line I need and then invoking the function itself.
So, why do I feel like I've just committed a sin?
Codeproject