From ee88a562433e75a0dd500ed7c71376d279eaacdf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Scott=20Gonz=C3=A1lez?= Date: Thu, 3 Feb 2011 16:37:17 -0500 Subject: [PATCH] Widget: Added ability to use $.widget() to create extensions. Fixes #6937 - Widget: Allow redefining a widget to create extensions. --- tests/unit/widget/widget_core.js | 21 +++++++++++++++++++++ ui/jquery.ui.widget.js | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/unit/widget/widget_core.js b/tests/unit/widget/widget_core.js index 6f4930621..186c407ed 100644 --- a/tests/unit/widget/widget_core.js +++ b/tests/unit/widget/widget_core.js @@ -821,4 +821,25 @@ test( "auto-destroy - .detach()", function() { $( "#widget" ).testWidget().detach(); }); +test( "redefine", function() { + expect( 4 ); + $.widget( "ui.testWidget", { + method: function( str ) { + strictEqual( this, instance, "original invoked with correct this" ); + equal( str, "bar", "original invoked with correct parameter" ); + } + }); + var ctor = $.ui.testWidget; + $.widget( "ui.testWidget", $.ui.testWidget, { + method: function( str ) { + equal( str, "foo", "new invoked with correct parameter" ); + this._super( "method", "bar" ); + } + }); + + var instance = new $.ui.testWidget(); + instance.method( "foo" ); + equal( $.ui.testWidget, ctor, "constructor did not change" ); +}); + }( jQuery ) ); diff --git a/ui/jquery.ui.widget.js b/ui/jquery.ui.widget.js index 1ec934469..6b84f7a9c 100644 --- a/ui/jquery.ui.widget.js +++ b/ui/jquery.ui.widget.js @@ -36,7 +36,7 @@ $.widget = function( name, base, prototype ) { }; $[ namespace ] = $[ namespace ] || {}; - $[ namespace ][ name ] = function( options, element ) { + $[ namespace ][ name ] = $[ namespace ][ name ] || function( options, element ) { // allow instantiation without "new" keyword if ( !this._createWidget ) { return new $[ namespace ][ name ]( options, element ); -- 2.39.5