Skip to content

Commit 32dbee6

Browse files
committed
Widget: Make _addClass auto remove classes in destroy
1 parent 769f68e commit 32dbee6

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

ui/widget.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,15 @@ $.Widget.prototype = {
292292
_init: $.noop,
293293

294294
destroy: function() {
295+
var that = this;
295296
this._destroy();
297+
298+
this._gcClasses();
299+
300+
$.each( this.classObject, function( key, value ) {
301+
that._removeClass( value, key );
302+
});
303+
296304
// we can probably remove the unbind calls in 2.0
297305
// all event bindings should go through this._on()
298306
this.element
@@ -304,9 +312,7 @@ $.Widget.prototype = {
304312
this.widget()
305313
.unbind( this.eventNamespace )
306314
.removeAttr( "aria-disabled" )
307-
.removeClass(
308-
this.widgetFullName + "-disabled " +
309-
"ui-state-disabled" );
315+
.removeClass( this.widgetFullName + "-disabled " + "ui-state-disabled" );
310316

311317
// clean up events and states
312318
this.bindings.unbind( this.eventNamespace );
@@ -401,25 +407,39 @@ $.Widget.prototype = {
401407
},
402408

403409
_constructClasses: function( element, keys, add, object ) {
404-
var i,
410+
var i, current,
405411
full = [],
406412
keyArray = keys.split( " " );
407413

408414
object = object || this.options.classes;
409415

410416
for ( i = 0; i < keyArray.length; i++ ) {
411-
this.classObject[ keyArray[ i ] ] = this.classObject[ keyArray[ i ] ] || $();
412-
this.classObject[ keyArray[ i ] ][ add ? "add" : "filter" ]( element );
413-
417+
current = this.classObject[ keyArray[ i ] ];
418+
this.classObject[ keyArray[ i ] ] = this.classObject[ keyArray[ i ] ] ? this.classObject[ keyArray[ i ] ].add( element ) : $().add( element );
414419
full.push( keyArray[ i ] );
415420

416421
if ( this.options.classes[ keyArray[ i ] ] ) {
417-
full.push( this.options.classes[ keyArray[ i ] ] );
422+
full.push( object[ keyArray[ i ] ] );
418423
}
419424
}
420425
return full.join( " " );
421426
},
422427

428+
_gcClasses: function( keys ) {
429+
var that = this;
430+
keys = keys ? keys.split( " " ) : this.classObject;
431+
$.each( keys, function( key, value ) {
432+
var classKey = ( typeof value === "string" ) ? value : key;
433+
if ( classKey && that.classObject[ classKey ] ) {
434+
that.classObject[ classKey ].each(function() {
435+
if ( this !== that.element[ 0 ] && ( !this.parentNode || !this.parentNode.parentNode ) ) {
436+
that.classObject[ classKey ] = that.classObject[ classKey ].filter( this );
437+
}
438+
});
439+
}
440+
});
441+
},
442+
423443
_removeClass: function( element, keys, extra ) {
424444
if ( typeof element === "string" ) {
425445
extra = keys;

0 commit comments

Comments
 (0)