]> source.dussan.org Git - jquery-ui.git/commitdiff
Generate a uuid for each widget for unique namespaces. Fixes #8385 - Widget: _bind...
authorJörn Zaefferer <joern.zaefferer@gmail.com>
Sun, 10 Jun 2012 18:55:04 +0000 (20:55 +0200)
committerScott González <scott.gonzalez@gmail.com>
Thu, 14 Jun 2012 14:39:41 +0000 (10:39 -0400)
tests/unit/widget/widget_core.js
ui/jquery.ui.widget.js

index 3dfaf1918b4264178179a35b43f59d7f4995dd34..1e40c40802efd6bec64dc3a0149de6e8505e19b0 100644 (file)
@@ -711,9 +711,10 @@ test( "_on() with delegate", function() {
        expect( 8 );
        $.widget( "ui.testWidget", {
                _create: function() {
+                       var uuid = this.uuid;
                        this.element = {
                                bind: function( event, handler ) {
-                                       equal( event, "click.testWidget" );
+                                       equal( event, "click.testWidget" + uuid );
                                        ok( $.isFunction(handler) );
                                },
                                trigger: $.noop
@@ -722,7 +723,7 @@ test( "_on() with delegate", function() {
                                return {
                                        delegate: function( selector, event, handler ) {
                                                equal( selector, "a" );
-                                               equal( event, "click.testWidget" );
+                                               equal( event, "click.testWidget" + uuid );
                                                ok( $.isFunction(handler) );
                                        }
                                };
@@ -735,7 +736,7 @@ test( "_on() with delegate", function() {
                                return {
                                        delegate: function( selector, event, handler ) {
                                                equal( selector, "form fieldset > input" );
-                                               equal( event, "change.testWidget" );
+                                               equal( event, "change.testWidget" + uuid );
                                                ok( $.isFunction(handler) );
                                        }
                                };
@@ -748,6 +749,24 @@ test( "_on() with delegate", function() {
        $.ui.testWidget();
 });
 
+test( "_bind() to common element", function() {
+       expect( 1 );
+       $.widget( "ui.testWidget", {
+               _create: function() {
+                       this._bind( this.document, {
+                               "customevent": "_handler"
+                       });
+               },
+               _handler: function() {
+                       ok( true, "handler triggered" );
+               }
+       });
+       var widget = $( "#widget" ).testWidget().data( "testWidget" );
+       $( "#widget-wrapper" ).testWidget();
+       widget.destroy();
+       $( document ).trigger( "customevent" );
+});
+
 test( "._hoverable()", function() {
        $.widget( "ui.testWidget", {
                _create: function() {
index 410b7003a9ccdb367f0a6a9d72a6dd556551e648..d5e68683e24b58f67cae85b13460daca51ff352d 100644 (file)
@@ -9,7 +9,8 @@
  */
 (function( $, undefined ) {
 
-var slice = Array.prototype.slice,
+var uuid = 0,
+       slice = Array.prototype.slice,
        _cleanData = $.cleanData;
 $.cleanData = function( elems ) {
        for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
@@ -214,6 +215,7 @@ $.Widget.prototype = {
                        this.options,
                        this._getCreateOptions(),
                        options );
+               this.uuid = uuid++;
 
                this.bindings = $();
                this.hoverable = $();
@@ -247,7 +249,7 @@ $.Widget.prototype = {
                // we can probably remove the unbind calls in 2.0
                // all event bindings should go through this._on()
                this.element
-                       .unbind( "." + this.widgetName )
+                       .unbind( "." + this.widgetName + this.uuid )
                        // 1.9 BC for #7810
                        // TODO remove dual storage
                        .removeData( this.widgetName )
@@ -256,14 +258,14 @@ $.Widget.prototype = {
                        // http://bugs.jquery.com/ticket/9413
                        .removeData( $.camelCase( this.widgetFullName ) );
                this.widget()
-                       .unbind( "." + this.widgetName )
+                       .unbind( "." + this.widgetName + this.uuid )
                        .removeAttr( "aria-disabled" )
                        .removeClass(
                                this.widgetFullName + "-disabled " +
                                "ui-state-disabled" );
 
                // clean up events and states
-               this.bindings.unbind( "." + this.widgetName );
+               this.bindings.unbind( "." + this.widgetName + this.uuid );
                this.hoverable.removeClass( "ui-state-hover" );
                this.focusable.removeClass( "ui-state-focus" );
        },
@@ -374,7 +376,7 @@ $.Widget.prototype = {
                        }
 
                        var match = event.match( /^(\w+)\s*(.*)$/ ),
-                               eventName = match[1] + "." + instance.widgetName,
+                               eventName = match[1] + "." + instance.widgetName + instance.uuid,
                                selector = match[2];
                        if ( selector ) {
                                instance.widget().delegate( selector, eventName, handlerProxy );