X Tutup
Skip to content

Commit 1438922

Browse files
committed
fix(class): correctly clean up on destroy
Fixes #3249 Closes #3256
1 parent f1e4292 commit 1438922

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

modules/angular2/src/directives/class.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ import {ListWrapper, StringMapWrapper, isListLikeIterable} from 'angular2/src/fa
2828
* </div>
2929
* ```
3030
*/
31-
@Directive(
32-
{selector: '[class]', lifecycle: [LifecycleEvent.onCheck], properties: ['rawClass: class']})
31+
@Directive({
32+
selector: '[class]',
33+
lifecycle: [LifecycleEvent.onCheck, LifecycleEvent.onDestroy],
34+
properties: ['rawClass: class']
35+
})
3336
export class CSSClass {
3437
_pipe: Pipe;
3538
_rawClass;
@@ -58,6 +61,8 @@ export class CSSClass {
5861
}
5962
}
6063

64+
onDestroy(): void { this._cleanupClasses(this._rawClass); }
65+
6166
private _cleanupClasses(rawClassVal): void {
6267
if (isPresent(rawClassVal)) {
6368
if (isListLikeIterable(rawClassVal)) {

modules/angular2/test/directives/class_spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,36 @@ import {
1414
xit,
1515
} from 'angular2/test_lib';
1616
import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
17-
import {Component, View} from 'angular2/angular2';
17+
import {Component, View, NgFor, bind} from 'angular2/angular2';
1818
import {DOM} from 'angular2/src/dom/dom_adapter';
1919
import {CSSClass} from 'angular2/src/directives/class';
20+
import {APP_VIEW_POOL_CAPACITY} from 'angular2/src/core/compiler/view_pool';
2021

2122
export function main() {
2223
describe('binding to CSS class list', () => {
2324

25+
describe('viewpool support', () => {
26+
beforeEachBindings(() => { return [bind(APP_VIEW_POOL_CAPACITY).toValue(100)]; });
27+
28+
it('should clean up when the directive is destroyed',
29+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
30+
var template = '<div *ng-for="var item of items" [class]="item"></div>';
31+
tcb.overrideTemplate(TestComponent, template)
32+
.createAsync(TestComponent)
33+
.then((rootTC) => {
34+
rootTC.componentInstance.items = [['0']];
35+
rootTC.detectChanges();
36+
rootTC.componentInstance.items = [['1']];
37+
rootTC.detectChanges();
38+
expect(rootTC.componentViewChildren[1].nativeElement.className)
39+
.toEqual('ng-binding 1');
40+
41+
async.done();
42+
});
43+
}));
44+
});
45+
46+
2447
describe('expressions evaluating to objects', () => {
2548

2649
it('should add classes specified in an object literal',
@@ -344,9 +367,10 @@ export function main() {
344367
}
345368

346369
@Component({selector: 'test-cmp'})
347-
@View({directives: [CSSClass]})
370+
@View({directives: [CSSClass, NgFor]})
348371
class TestComponent {
349372
condition: boolean = true;
373+
items: any[];
350374
arrExpr: List<string> = ['foo'];
351375
objExpr = {'foo': true, 'bar': false};
352376
strExpr = 'foo';

0 commit comments

Comments
 (0)
X Tutup