Skip to content

Commit 25dae41

Browse files
committed
Widget: Added _super() and _superApply() methods. Fixes #6861 - Widget: Add _super() and _superApply() for easy access to parent methods.
1 parent f711e36 commit 25dae41

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

tests/unit/widget/widget_core.js

+53
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,59 @@ test( "re-init", function() {
228228
same( actions, [ "optionfoo", "init" ], "correct methods called on re-init with options" );
229229
});
230230

231+
test( "._super()", function() {
232+
expect( 6 );
233+
var instance;
234+
$.widget( "ui.testWidget", {
235+
method: function( a, b ) {
236+
same( this, instance, "this is correct in super widget" );
237+
same( a, 5, "parameter passed to super widget" );
238+
same( b, 10, "second parameter passed to super widget" );
239+
return a + b;
240+
}
241+
});
242+
243+
$.widget( "ui.testWidget2", $.ui.testWidget, {
244+
method: function( a ) {
245+
same( this, instance, "this is correct in widget" );
246+
same( a, 5, "parameter passed to widget" );
247+
var ret = this._super( "method", a, a*2 );
248+
same( ret, 15, "super returned value" );
249+
}
250+
});
251+
252+
instance = $( "<div>" ).testWidget2().data( "testWidget2" );
253+
instance.method( 5 );
254+
delete $.ui.testWidget2;
255+
});
256+
257+
test( "._superApply()", function() {
258+
expect( 7 );
259+
var instance;
260+
$.widget( "ui.testWidget", {
261+
method: function( a, b ) {
262+
same( this, instance, "this is correct in super widget" );
263+
same( a, 5, "parameter passed to super widget" );
264+
same( b, 10, "second parameter passed to super widget" );
265+
return a + b;
266+
}
267+
});
268+
269+
$.widget( "ui.testWidget2", $.ui.testWidget, {
270+
method: function( a, b ) {
271+
same( this, instance, "this is correct in widget" );
272+
same( a, 5, "parameter passed to widget" );
273+
same( b, 10, "second parameter passed to widget" );
274+
var ret = this._superApply( "method", arguments );
275+
same( ret, 15, "super returned value" );
276+
}
277+
});
278+
279+
instance = $( "<div>" ).testWidget2().data( "testWidget2" );
280+
instance.method( 5, 10 );
281+
delete $.ui.testWidget2;
282+
});
283+
231284
test( ".option() - getter", function() {
232285
$.widget( "ui.testWidget", {
233286
_create: function() {}

ui/jquery.ui.widget.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
(function( $, undefined ) {
1111

12+
var slice = Array.prototype.slice;
13+
1214
var _cleanData = $.cleanData;
1315
$.cleanData = function( elems ) {
1416
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
@@ -50,7 +52,8 @@ $.widget = function( name, base, prototype ) {
5052
namespace: namespace,
5153
widgetName: name,
5254
widgetEventPrefix: name,
53-
widgetBaseClass: fullName
55+
widgetBaseClass: fullName,
56+
base: base.prototype
5457
}, prototype );
5558

5659
$.widget.bridge( name, $[ namespace ][ name ] );
@@ -59,7 +62,7 @@ $.widget = function( name, base, prototype ) {
5962
$.widget.bridge = function( name, object ) {
6063
$.fn[ name ] = function( options ) {
6164
var isMethodCall = typeof options === "string",
62-
args = Array.prototype.slice.call( arguments, 1 ),
65+
args = slice.call( arguments, 1 ),
6366
returnValue = this;
6467

6568
// allow multiple hashes to be passed on init
@@ -141,6 +144,13 @@ $.Widget.prototype = {
141144
_create: function() {},
142145
_init: function() {},
143146

147+
_super: function( method ) {
148+
return this.base[ method ].apply( this, slice.call( arguments, 1 ) );
149+
},
150+
_superApply: function( method, args ) {
151+
return this.base[ method ].apply( this, args );
152+
},
153+
144154
destroy: function() {
145155
this.element
146156
.unbind( "." + this.widgetName )

0 commit comments

Comments
 (0)