mirror of
https://github.com/jquery/jquery-ui.git
synced 2024-11-21 11:04:24 +00:00
Widget: Bridge falls back to name if there is no widgetFullName, and always stores instances in data. Fixed #8775 - Widget: Bridge fails if widgetFullName is not supplied.
(cherry picked from commit 75bd22eb73
)
This commit is contained in:
parent
78ad8e4891
commit
8f098d8806
@ -4,6 +4,7 @@ module( "widget factory", {
|
|||||||
teardown: function() {
|
teardown: function() {
|
||||||
if ( $.ui ) {
|
if ( $.ui ) {
|
||||||
delete $.ui.testWidget;
|
delete $.ui.testWidget;
|
||||||
|
delete $.fn.testWidget;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -1305,4 +1306,56 @@ asyncTest( "_delay", function() {
|
|||||||
$( "#widget" ).testWidget();
|
$( "#widget" ).testWidget();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test( "$.widget.bridge()", function() {
|
||||||
|
expect( 9 );
|
||||||
|
|
||||||
|
var instance, ret,
|
||||||
|
elem = $( "<div>" );
|
||||||
|
|
||||||
|
function TestWidget( options, element ) {
|
||||||
|
deepEqual( options, { foo: "bar" }, "options passed" );
|
||||||
|
strictEqual( element, elem[ 0 ], "element passed" );
|
||||||
|
}
|
||||||
|
|
||||||
|
$.extend( TestWidget.prototype, {
|
||||||
|
method: function( param ) {
|
||||||
|
ok( true, "method called via .pluginName(methodName)" );
|
||||||
|
equal( param, "value1",
|
||||||
|
"parameter passed via .pluginName(methodName, param)" );
|
||||||
|
},
|
||||||
|
getter: function() {
|
||||||
|
return "qux";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$.widget.bridge( "testWidget", TestWidget );
|
||||||
|
|
||||||
|
ok( $.isFunction( $.fn.testWidget ), "jQuery plugin was created" );
|
||||||
|
|
||||||
|
strictEqual( elem.testWidget({ foo: "bar" }), elem, "plugin returns original jQuery object" );
|
||||||
|
instance = elem.data( "testWidget" );
|
||||||
|
equal( typeof instance, "object", "instance stored in .data(pluginName)" );
|
||||||
|
|
||||||
|
ret = elem.testWidget( "method", "value1" );
|
||||||
|
equal( ret, elem, "jQuery object returned from method call" );
|
||||||
|
|
||||||
|
ret = elem.testWidget( "getter" );
|
||||||
|
equal( ret, "qux", "getter returns value" );
|
||||||
|
});
|
||||||
|
|
||||||
|
test( "$.widget.bridge() - widgetFullName", function() {
|
||||||
|
expect( 1 );
|
||||||
|
|
||||||
|
var instance,
|
||||||
|
elem = $( "<div>" );
|
||||||
|
|
||||||
|
function TestWidget() {}
|
||||||
|
TestWidget.prototype.widgetFullName = "custom-widget";
|
||||||
|
$.widget.bridge( "testWidget", TestWidget );
|
||||||
|
|
||||||
|
elem.testWidget();
|
||||||
|
instance = elem.data( "custom-widget" );
|
||||||
|
equal( typeof instance, "object", "instance stored in .data(widgetFullName)" );
|
||||||
|
});
|
||||||
|
|
||||||
}( jQuery ) );
|
}( jQuery ) );
|
||||||
|
4
ui/jquery.ui.widget.js
vendored
4
ui/jquery.ui.widget.js
vendored
@ -160,7 +160,7 @@ $.widget.extend = function( target ) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
$.widget.bridge = function( name, object ) {
|
$.widget.bridge = function( name, object ) {
|
||||||
var fullName = object.prototype.widgetFullName;
|
var fullName = object.prototype.widgetFullName || name;
|
||||||
$.fn[ name ] = function( options ) {
|
$.fn[ name ] = function( options ) {
|
||||||
var isMethodCall = typeof options === "string",
|
var isMethodCall = typeof options === "string",
|
||||||
args = slice.call( arguments, 1 ),
|
args = slice.call( arguments, 1 ),
|
||||||
@ -196,7 +196,7 @@ $.widget.bridge = function( name, object ) {
|
|||||||
if ( instance ) {
|
if ( instance ) {
|
||||||
instance.option( options || {} )._init();
|
instance.option( options || {} )._init();
|
||||||
} else {
|
} else {
|
||||||
new object( options, this );
|
$.data( this, fullName, new object( options, this ) );
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user