]> source.dussan.org Git - jquery-ui.git/commitdiff
Widget: Hook into jQuery.cleanData to auto-destroy widgets. Fixes #6008 - Widget...
authorScott González <scott.gonzalez@gmail.com>
Fri, 3 Sep 2010 13:49:29 +0000 (09:49 -0400)
committerScott González <scott.gonzalez@gmail.com>
Fri, 3 Sep 2010 13:49:29 +0000 (09:49 -0400)
tests/unit/widget/widget.html
tests/unit/widget/widget_core.js
ui/jquery.ui.widget.js

index 712f14409e6973a11fd0eb1d84d0ae9e236a86c6..9f7a0f04015dba2f28a917b5ce568f6ad00d8d7c 100644 (file)
@@ -29,7 +29,9 @@
 <div id="main" style="position: absolute; top: -10000px; left: -10000px;">
 
 <div id="widget-wrapper">
-       <div id="widget"></div>
+       <div id="widget">
+               <div>...</div>
+       </div>
 </div>
 
 </div>
index 2d5502838c524895bb7088c9d722a601e5df7e74..6042221064c4160d6af80870c661c12ce410dbd4 100644 (file)
@@ -409,4 +409,71 @@ test( "._trigger() - provide event and ui", function() {
        .testWidget( "testEvent" );
 });
 
+test( "auto-destroy - .remove()", function() {
+       expect( 1 );
+       $.widget( "ui.testWidget", {
+               _create: function() {},
+               destroy: function() {
+                       ok( true, "destroyed from .remove()" );
+               }
+       });
+       $( "#widget" ).testWidget().remove();
+});
+
+test( "auto-destroy - .remove() on parent", function() {
+       expect( 1 );
+       $.widget( "ui.testWidget", {
+               _create: function() {},
+               destroy: function() {
+                       ok( true, "destroyed from .remove() on parent" );
+               }
+       });
+       $( "#widget" ).testWidget().parent().remove();
+});
+
+test( "auto-destroy - .remove() on child", function() {
+       $.widget( "ui.testWidget", {
+               _create: function() {},
+               destroy: function() {
+                       ok( false, "destroyed from .remove() on child" );
+               }
+       });
+       $( "#widget" ).testWidget().children().remove();
+       // http://github.com/jquery/qunit/pull/34
+       $.ui.testWidget.prototype.destroy = $.noop;
+});
+
+test( "auto-destroy - .empty()", function() {
+       $.widget( "ui.testWidget", {
+               _create: function() {},
+               destroy: function() {
+                       ok( false, "destroyed from .empty()" );
+               }
+       });
+       $( "#widget" ).testWidget().empty();
+       // http://github.com/jquery/qunit/pull/34
+       $.ui.testWidget.prototype.destroy = $.noop;
+});
+
+test( "auto-destroy - .empty() on parent", function() {
+       expect( 1 );
+       $.widget( "ui.testWidget", {
+               _create: function() {},
+               destroy: function() {
+                       ok( true, "destroyed from .empty() on parent" );
+               }
+       });
+       $( "#widget" ).testWidget().parent().empty();
+});
+
+test( "auto-destroy - .detach()", function() {
+       $.widget( "ui.testWidget", {
+               _create: function() {},
+               destroy: function() {
+                       ok( false, "destroyed from .detach()" );
+               }
+       });
+       $( "#widget" ).testWidget().detach();
+});
+
 })( jQuery );
index d08dbb81f7cbbc464d2e44a63a01f99b2f2a6ec1..dbfea2574228274bff039d9574aacd828bf533f8 100644 (file)
@@ -9,20 +9,30 @@
  */
 (function( $, undefined ) {
 
-var _remove = $.fn.remove;
-
-$.fn.remove = function( selector, keepData ) {
-       return this.each(function() {
-               if ( !keepData ) {
-                       if ( !selector || $.filter( selector, [ this ] ).length ) {
-                               $( "*", this ).add( [ this ] ).each(function() {
-                                       $( this ).triggerHandler( "remove" );
-                               });
-                       }
+// jQuery 1.4+
+if ( $.cleanData ) {
+       var _cleanData = $.cleanData;
+       $.cleanData = function( elems ) {
+               for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
+                       $( elem ).triggerHandler( "remove" );
                }
-               return _remove.call( $(this), selector, keepData );
-       });
-};
+               _cleanData( elems );
+       };
+} else {
+       var _remove = $.fn.remove;
+       $.fn.remove = function( selector, keepData ) {
+               return this.each(function() {
+                       if ( !keepData ) {
+                               if ( !selector || $.filter( selector, [ this ] ).length ) {
+                                       $( "*", this ).add( [ this ] ).each(function() {
+                                               $( this ).triggerHandler( "remove" );
+                                       });
+                               }
+                       }
+                       return _remove.call( $(this), selector, keepData );
+               });
+       };
+}
 
 $.widget = function( name, base, prototype ) {
        var namespace = name.split( "." )[ 0 ],