mirror of
https://github.com/jquery/jquery-ui.git
synced 2024-10-05 01:44:18 +00:00
Widget: Added _super() and _superApply() methods. Fixes #6861 - Widget: Add _super() and _superApply() for easy access to parent methods.
This commit is contained in:
parent
f711e362cd
commit
25dae411bc
@ -228,6 +228,59 @@ test( "re-init", function() {
|
||||
same( actions, [ "optionfoo", "init" ], "correct methods called on re-init with options" );
|
||||
});
|
||||
|
||||
test( "._super()", function() {
|
||||
expect( 6 );
|
||||
var instance;
|
||||
$.widget( "ui.testWidget", {
|
||||
method: function( a, b ) {
|
||||
same( this, instance, "this is correct in super widget" );
|
||||
same( a, 5, "parameter passed to super widget" );
|
||||
same( b, 10, "second parameter passed to super widget" );
|
||||
return a + b;
|
||||
}
|
||||
});
|
||||
|
||||
$.widget( "ui.testWidget2", $.ui.testWidget, {
|
||||
method: function( a ) {
|
||||
same( this, instance, "this is correct in widget" );
|
||||
same( a, 5, "parameter passed to widget" );
|
||||
var ret = this._super( "method", a, a*2 );
|
||||
same( ret, 15, "super returned value" );
|
||||
}
|
||||
});
|
||||
|
||||
instance = $( "<div>" ).testWidget2().data( "testWidget2" );
|
||||
instance.method( 5 );
|
||||
delete $.ui.testWidget2;
|
||||
});
|
||||
|
||||
test( "._superApply()", function() {
|
||||
expect( 7 );
|
||||
var instance;
|
||||
$.widget( "ui.testWidget", {
|
||||
method: function( a, b ) {
|
||||
same( this, instance, "this is correct in super widget" );
|
||||
same( a, 5, "parameter passed to super widget" );
|
||||
same( b, 10, "second parameter passed to super widget" );
|
||||
return a + b;
|
||||
}
|
||||
});
|
||||
|
||||
$.widget( "ui.testWidget2", $.ui.testWidget, {
|
||||
method: function( a, b ) {
|
||||
same( this, instance, "this is correct in widget" );
|
||||
same( a, 5, "parameter passed to widget" );
|
||||
same( b, 10, "second parameter passed to widget" );
|
||||
var ret = this._superApply( "method", arguments );
|
||||
same( ret, 15, "super returned value" );
|
||||
}
|
||||
});
|
||||
|
||||
instance = $( "<div>" ).testWidget2().data( "testWidget2" );
|
||||
instance.method( 5, 10 );
|
||||
delete $.ui.testWidget2;
|
||||
});
|
||||
|
||||
test( ".option() - getter", function() {
|
||||
$.widget( "ui.testWidget", {
|
||||
_create: function() {}
|
||||
|
14
ui/jquery.ui.widget.js
vendored
14
ui/jquery.ui.widget.js
vendored
@ -9,6 +9,8 @@
|
||||
*/
|
||||
(function( $, undefined ) {
|
||||
|
||||
var slice = Array.prototype.slice;
|
||||
|
||||
var _cleanData = $.cleanData;
|
||||
$.cleanData = function( elems ) {
|
||||
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
|
||||
@ -50,7 +52,8 @@ $.widget = function( name, base, prototype ) {
|
||||
namespace: namespace,
|
||||
widgetName: name,
|
||||
widgetEventPrefix: name,
|
||||
widgetBaseClass: fullName
|
||||
widgetBaseClass: fullName,
|
||||
base: base.prototype
|
||||
}, prototype );
|
||||
|
||||
$.widget.bridge( name, $[ namespace ][ name ] );
|
||||
@ -59,7 +62,7 @@ $.widget = function( name, base, prototype ) {
|
||||
$.widget.bridge = function( name, object ) {
|
||||
$.fn[ name ] = function( options ) {
|
||||
var isMethodCall = typeof options === "string",
|
||||
args = Array.prototype.slice.call( arguments, 1 ),
|
||||
args = slice.call( arguments, 1 ),
|
||||
returnValue = this;
|
||||
|
||||
// allow multiple hashes to be passed on init
|
||||
@ -141,6 +144,13 @@ $.Widget.prototype = {
|
||||
_create: function() {},
|
||||
_init: function() {},
|
||||
|
||||
_super: function( method ) {
|
||||
return this.base[ method ].apply( this, slice.call( arguments, 1 ) );
|
||||
},
|
||||
_superApply: function( method, args ) {
|
||||
return this.base[ method ].apply( this, args );
|
||||
},
|
||||
|
||||
destroy: function() {
|
||||
this.element
|
||||
.unbind( "." + this.widgetName )
|
||||
|
Loading…
Reference in New Issue
Block a user