[1.7.2] Extending UI widgets - Inheritance Approach
Hi
I know it is possible to extend jQuery UI widget to include new methods or overwrite methods. Here is an example of re-writing the "_refreshValue" method of the progressbar widget to display the value as text on the widget:
- (function($) {
- $.extend($.ui.progressbar.prototype, {
- _refreshValue: function() {
- var value = this.value();
- this.valueDiv[value == this._valueMax() ? 'addClass' : 'removeClass']("ui-corner-right");
- this.valueDiv.width(value + '%');
- this.element.attr("aria-valuenow", value);
- this.valueDiv.text(value + '%'); // New/diff Code
- }
- });
- })(jQuery);
From the code above, it is clear that if you want to retain all of the functionality of the original code but only want to add to it, one need to duplicate the original code. The ideal would have been if one could call the original method from within the new method. Something similar to the way one call the widget factory methods.
In essence, I'd like to code it like so:
- (function($) {
- $.extend($.ui.progressbar.prototype, {
- _refreshValue: function() {
- $.ui.progressbar.prototype._refreshValue.call(this);
- this.valueDiv.text(value + '%');
- }
- });
- });
Note the code above won't work because of recursion.
Has anyone ever attempted anything like this before?