Widget Bridge: Make the _init method optional. Add tests for both states. Fixes #9543 - Widget bridge: Make _init() optional.

This commit is contained in:
Jörn Zaefferer 2013-09-11 22:11:58 +02:00
parent 37bba1eb92
commit 6e799c39d3
2 changed files with 16 additions and 2 deletions

View File

@ -1409,7 +1409,7 @@ asyncTest( "_delay", function() {
}); });
test( "$.widget.bridge()", function() { test( "$.widget.bridge()", function() {
expect( 10 ); expect( 14 );
var instance, ret, var instance, ret,
elem = $( "<div>" ); elem = $( "<div>" );
@ -1427,6 +1427,9 @@ test( "$.widget.bridge()", function() {
}, },
getter: function() { getter: function() {
return "qux"; return "qux";
},
option: function( options ) {
deepEqual( options, {} );
} }
}); });
@ -1444,6 +1447,14 @@ test( "$.widget.bridge()", function() {
ret = elem.testWidget( "getter" ); ret = elem.testWidget( "getter" );
equal( ret, "qux", "getter returns value" ); equal( ret, "qux", "getter returns value" );
elem.testWidget();
ok( true, "_init is optional" );
TestWidget.prototype._init = function() {
ok( "_init", "_init now exists, so its called" );
};
elem.testWidget();
}); });
test( "$.widget.bridge() - widgetFullName", function() { test( "$.widget.bridge() - widgetFullName", function() {

View File

@ -203,7 +203,10 @@ $.widget.bridge = function( name, object ) {
this.each(function() { this.each(function() {
var instance = $.data( this, fullName ); var instance = $.data( this, fullName );
if ( instance ) { if ( instance ) {
instance.option( options || {} )._init(); instance.option( options || {} );
if ( instance._init ) {
instance._init();
}
} else { } else {
$.data( this, fullName, new object( options, this ) ); $.data( this, fullName, new object( options, this ) );
} }