Skip to content
Permalink
Browse files

Update data naming for Widget, including backwards compability. Updat…

…ed tests and added one for the custom expression. Partial fix for #7810
  • Loading branch information...
jzaefferer committed Mar 1, 2012
1 parent 45ca20f commit 5fda4f6828ad523bd0692b76f903010a458fcac4
Showing with 39 additions and 17 deletions.
  1. +28 −14 tests/unit/widget/widget_core.js
  2. +11 −3 ui/jquery.ui.widget.js
@@ -32,36 +32,36 @@ test( "element normalization", function() {
// workaround for core ticket #8381
this.element.appendTo( "#qunit-fixture" );
ok( this.element.is( "div" ), "generated div" );
deepEqual( this.element.data( "testWidget" ), this, "intance stored in .data()" );
deepEqual( this.element.data( "ui-testWidget" ), this, "instance stored in .data()" );

This comment has been minimized.

Copy link
@sobrinho

sobrinho Mar 2, 2012

I guess you mean ui-test-widget :P

This comment has been minimized.

Copy link
@scottgonzalez

scottgonzalez Mar 2, 2012

Member

No. The namespace is ui and the name is testWidget, so the key is ui-testWidget.

This comment has been minimized.

Copy link
@sobrinho
};
$.ui.testWidget();

$.ui.testWidget.prototype.defaultElement = "<span data-test='pass'></span>";
$.ui.testWidget.prototype._create = function() {
ok( this.element.is( "span[data-test=pass]" ), "generated span with properties" );
deepEqual( this.element.data( "testWidget" ), this, "instace stored in .data()" );
deepEqual( this.element.data( "ui-testWidget" ), this, "instace stored in .data()" );
};
$.ui.testWidget();

elem = $( "<input>" );
$.ui.testWidget.prototype._create = function() {
deepEqual( this.element[ 0 ], elem[ 0 ], "from element" );
deepEqual( elem.data( "testWidget" ), this, "instace stored in .data()" );
deepEqual( elem.data( "ui-testWidget" ), this, "instace stored in .data()" );
};
$.ui.testWidget( {}, elem[ 0 ] );

elem = $( "<div>" );
$.ui.testWidget.prototype._create = function() {
deepEqual( this.element[ 0 ], elem[ 0 ], "from jQuery object" );
deepEqual( elem.data( "testWidget" ), this, "instace stored in .data()" );
deepEqual( elem.data( "ui-testWidget" ), this, "instace stored in .data()" );
};
$.ui.testWidget( {}, elem );

elem = $( "<div id='element-normalization-selector'></div>" )
.appendTo( "#qunit-fixture" );
$.ui.testWidget.prototype._create = function() {
deepEqual( this.element[ 0 ], elem[ 0 ], "from selector" );
deepEqual( elem.data( "testWidget" ), this, "instace stored in .data()" );
deepEqual( elem.data( "ui-testWidget" ), this, "instace stored in .data()" );
};
$.ui.testWidget( {}, "#element-normalization-selector" );

@@ -73,8 +73,16 @@ test( "element normalization", function() {
$.ui.testWidget();
});

test( "custom selector expression", function() {
var elem = $( "<div>" ).appendTo( "#qunit-fixture" );
$.widget( "ui.testWidget", {} );
elem.testWidget();
deepEqual( $( ":ui-testWidget" )[0], elem[0] );
elem.testWidget( "destroy" );
});

test( "jQuery usage", function() {
expect( 13 );
expect( 15 );

var shouldCreate = false;

@@ -112,12 +120,18 @@ test( "jQuery usage", function() {
.testWidget();
shouldCreate = false;

var instance = elem.data( "testWidget" );
var instance = elem.data( "ui-testWidget" );
equal( typeof instance, "object", "instance stored in .data(pluginName)" );
equal( instance.element[0], elem[0], "element stored on widget" );
var ret = elem.testWidget( "methodWithParams", "value1", "value2" );
equal( ret, elem, "jQuery object returned from method call" );

// 1.9 BC for #7810
// TODO remove
var bcInstance = elem.data("testWidget");
equal( typeof bcInstance, "object", "instance stored in .data(pluginName)" );
equal( bcInstance.element[0], elem[0], "element stored on widget" );

ret = elem.testWidget( "getterSetterMethod" );
equal( ret, 5, "getter/setter can act as getter" );
ret = elem.testWidget( "getterSetterMethod", 30 );
@@ -160,7 +174,7 @@ test( "direct usage", function() {
var instance = new $.ui.testWidget( {}, elem );
shouldCreate = false;

equal( $( elem ).data( "testWidget" ), instance,
equal( $( elem ).data( "ui-testWidget" ), instance,
"instance stored in .data(pluginName)" );
equal( instance.element[ 0 ], elem, "element stored on widget" );

@@ -374,7 +388,7 @@ test( "._super()", function() {
}
});

instance = $( "<div>" ).testWidget3().data( "testWidget3" );
instance = $( "<div>" ).testWidget3().data( "ui-testWidget3" );
instance.method( 5 );
delete $.ui.testWidget3;
delete $.ui.testWidget2;
@@ -411,7 +425,7 @@ test( "._superApply()", function() {
}
});

instance = $( "<div>" ).testWidget3().data( "testWidget3" );
instance = $( "<div>" ).testWidget3().data( "ui-testWidget3" );
instance.method( 5, 10 );
delete $.ui.testWidget3;
delete $.ui.testWidget2;
@@ -521,7 +535,7 @@ test( ".option() - deep option setter", function() {
$.widget( "ui.testWidget", {} );
var div = $( "<div>" ).testWidget();
function deepOption( from, to, msg ) {
div.data( "testWidget" ).options.foo = from;
div.data( "ui-testWidget" ).options.foo = from;
$.ui.testWidget.prototype._setOption = function( key, value ) {
deepEqual( key, "foo", msg + ": key" );
deepEqual( value, to, msg + ": value" );
@@ -804,7 +818,7 @@ test( "._trigger() - no event, no ui", function() {
deepEqual( ui, {}, "empty ui hash passed" );
handlers.push( this );
});
deepEqual( $( "#widget" ).data( "testWidget" )._trigger( "foo" ), true,
deepEqual( $( "#widget" ).data( "ui-testWidget" )._trigger( "foo" ), true,
"_trigger returns true when event is not cancelled" );
deepEqual( handlers, [
$( "#widget" )[ 0 ],
@@ -832,7 +846,7 @@ test( "._trigger() - cancelled event", function() {
ok( true, "event was triggered" );
return false;
});
deepEqual( $( "#widget" ).data( "testWidget" )._trigger( "foo" ), false,
deepEqual( $( "#widget" ).data( "ui-testWidget" )._trigger( "foo" ), false,
"_trigger returns false when event is cancelled" );
});

@@ -846,7 +860,7 @@ test( "._trigger() - cancelled callback", function() {
return false;
}
});
deepEqual( $( "#widget" ).data( "testWidget" )._trigger( "foo" ), false,
deepEqual( $( "#widget" ).data( "ui-testWidget" )._trigger( "foo" ), false,
"_trigger returns false when callback returns false" );
});

@@ -36,7 +36,7 @@ $.widget = function( name, base, prototype ) {

// create selector for plugin
$.expr[ ":" ][ fullName ] = function( elem ) {
return !!$.data( elem, name );
return !!$.data( elem, fullName );
};

$[ namespace ] = $[ namespace ] || {};
@@ -148,6 +148,7 @@ $.widget.extend = function( target ) {
};

$.widget.bridge = function( name, object ) {
var fullName = object.prototype.widgetBaseClass;
$.fn[ name ] = function( options ) {
var isMethodCall = typeof options === "string",
args = slice.call( arguments, 1 ),
@@ -160,7 +161,9 @@ $.widget.bridge = function( name, object ) {

if ( isMethodCall ) {
this.each(function() {
var instance = $.data( this, name );
// 1.9 BC for #7810
// TODO remove fallback to name
var instance = $.data( this, fullName ) || $.data( this, name );
if ( !instance ) {
return $.error( "cannot call methods on " + name + " prior to initialization; " +
"attempted to call method '" + options + "'" );
@@ -178,7 +181,9 @@ $.widget.bridge = function( name, object ) {
});
} else {
this.each(function() {
var instance = $.data( this, name );
// 1.9 BC for #7810
// TODO remove fallback to name
var instance = $.data( this, fullName ) || $.data( this, name );
if ( instance ) {
instance.option( options || {} )._init();
} else {
@@ -217,7 +222,10 @@ $.Widget.prototype = {
this.focusable = $();

if ( element !== this ) {
// 1.9 BC for #7810
// TODO remove dual storage
$.data( element, this.widgetName, this );
$.data( element, this.widgetBaseClass, this );
this._bind({ remove: "destroy" });
this.document = $( element.style ?
// element within the document

0 comments on commit 5fda4f6

Please sign in to comment.
You can’t perform that action at this time.