Skip to content

Commit 0a0a39f

Browse files
committed
Widget: Hook into jQuery.cleanData to auto-destroy widgets. Fixes #6008 - Widget: auto-destroy is broken in jQuery 1.4.
1 parent e5f3bfc commit 0a0a39f

File tree

3 files changed

+93
-14
lines changed

3 files changed

+93
-14
lines changed

tests/unit/widget/widget.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ <h2 id="qunit-userAgent"></h2>
2929
<div id="main" style="position: absolute; top: -10000px; left: -10000px;">
3030

3131
<div id="widget-wrapper">
32-
<div id="widget"></div>
32+
<div id="widget">
33+
<div>...</div>
34+
</div>
3335
</div>
3436

3537
</div>

tests/unit/widget/widget_core.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,71 @@ test( "._trigger() - provide event and ui", function() {
409409
.testWidget( "testEvent" );
410410
});
411411

412+
test( "auto-destroy - .remove()", function() {
413+
expect( 1 );
414+
$.widget( "ui.testWidget", {
415+
_create: function() {},
416+
destroy: function() {
417+
ok( true, "destroyed from .remove()" );
418+
}
419+
});
420+
$( "#widget" ).testWidget().remove();
421+
});
422+
423+
test( "auto-destroy - .remove() on parent", function() {
424+
expect( 1 );
425+
$.widget( "ui.testWidget", {
426+
_create: function() {},
427+
destroy: function() {
428+
ok( true, "destroyed from .remove() on parent" );
429+
}
430+
});
431+
$( "#widget" ).testWidget().parent().remove();
432+
});
433+
434+
test( "auto-destroy - .remove() on child", function() {
435+
$.widget( "ui.testWidget", {
436+
_create: function() {},
437+
destroy: function() {
438+
ok( false, "destroyed from .remove() on child" );
439+
}
440+
});
441+
$( "#widget" ).testWidget().children().remove();
442+
// http://github.com/jquery/qunit/pull/34
443+
$.ui.testWidget.prototype.destroy = $.noop;
444+
});
445+
446+
test( "auto-destroy - .empty()", function() {
447+
$.widget( "ui.testWidget", {
448+
_create: function() {},
449+
destroy: function() {
450+
ok( false, "destroyed from .empty()" );
451+
}
452+
});
453+
$( "#widget" ).testWidget().empty();
454+
// http://github.com/jquery/qunit/pull/34
455+
$.ui.testWidget.prototype.destroy = $.noop;
456+
});
457+
458+
test( "auto-destroy - .empty() on parent", function() {
459+
expect( 1 );
460+
$.widget( "ui.testWidget", {
461+
_create: function() {},
462+
destroy: function() {
463+
ok( true, "destroyed from .empty() on parent" );
464+
}
465+
});
466+
$( "#widget" ).testWidget().parent().empty();
467+
});
468+
469+
test( "auto-destroy - .detach()", function() {
470+
$.widget( "ui.testWidget", {
471+
_create: function() {},
472+
destroy: function() {
473+
ok( false, "destroyed from .detach()" );
474+
}
475+
});
476+
$( "#widget" ).testWidget().detach();
477+
});
478+
412479
})( jQuery );

ui/jquery.ui.widget.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,30 @@
99
*/
1010
(function( $, undefined ) {
1111

12-
var _remove = $.fn.remove;
13-
14-
$.fn.remove = function( selector, keepData ) {
15-
return this.each(function() {
16-
if ( !keepData ) {
17-
if ( !selector || $.filter( selector, [ this ] ).length ) {
18-
$( "*", this ).add( [ this ] ).each(function() {
19-
$( this ).triggerHandler( "remove" );
20-
});
21-
}
12+
// jQuery 1.4+
13+
if ( $.cleanData ) {
14+
var _cleanData = $.cleanData;
15+
$.cleanData = function( elems ) {
16+
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
17+
$( elem ).triggerHandler( "remove" );
2218
}
23-
return _remove.call( $(this), selector, keepData );
24-
});
25-
};
19+
_cleanData( elems );
20+
};
21+
} else {
22+
var _remove = $.fn.remove;
23+
$.fn.remove = function( selector, keepData ) {
24+
return this.each(function() {
25+
if ( !keepData ) {
26+
if ( !selector || $.filter( selector, [ this ] ).length ) {
27+
$( "*", this ).add( [ this ] ).each(function() {
28+
$( this ).triggerHandler( "remove" );
29+
});
30+
}
31+
}
32+
return _remove.call( $(this), selector, keepData );
33+
});
34+
};
35+
}
2636

2737
$.widget = function( name, base, prototype ) {
2838
var namespace = name.split( "." )[ 0 ],

0 commit comments

Comments
 (0)