From 5fda4f6828ad523bd0692b76f903010a458fcac4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=CC=88rn=20Zaefferer?= Date: Thu, 1 Mar 2012 14:30:02 +0100 Subject: [PATCH] Update data naming for Widget, including backwards compability. Updated tests and added one for the custom expression. Partial fix for #7810 --- tests/unit/widget/widget_core.js | 42 +++++++++++++++++++++----------- ui/jquery.ui.widget.js | 14 ++++++++--- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/tests/unit/widget/widget_core.js b/tests/unit/widget/widget_core.js index c451c4cf6..b4bb6356d 100644 --- a/tests/unit/widget/widget_core.js +++ b/tests/unit/widget/widget_core.js @@ -32,28 +32,28 @@ 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()" ); }; $.ui.testWidget(); $.ui.testWidget.prototype.defaultElement = ""; $.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 = $( "" ); $.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 = $( "
" ); $.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 ); @@ -61,7 +61,7 @@ test( "element normalization", function() { .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 = $( "
" ).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 = $( "
" ).testWidget3().data( "testWidget3" ); + instance = $( "
" ).testWidget3().data( "ui-testWidget3" ); instance.method( 5 ); delete $.ui.testWidget3; delete $.ui.testWidget2; @@ -411,7 +425,7 @@ test( "._superApply()", function() { } }); - instance = $( "
" ).testWidget3().data( "testWidget3" ); + instance = $( "
" ).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 = $( "
" ).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" ); }); diff --git a/ui/jquery.ui.widget.js b/ui/jquery.ui.widget.js index 2d48ae26d..d11f993c6 100644 --- a/ui/jquery.ui.widget.js +++ b/ui/jquery.ui.widget.js @@ -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 -- 2.39.5