Skip to content

Commit 3ea0c7d

Browse files
author
saranya.r
committed
1 parent 9800176 commit 3ea0c7d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/htmlbars-compiler/tests/dirtying-test.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ function commonSetup() {
3636
}
3737
});
3838

39-
registerHelper('each', function(params) {
39+
registerHelper('each', function(params, hash, blocks) {
4040
var list = params[0];
41-
41+
if(list.length !== 0) {
4242
for (var i=0, l=list.length; i<l; i++) {
4343
var item = list[i];
4444
if (this.arity > 0) {
@@ -47,6 +47,9 @@ function commonSetup() {
4747
this.yieldItem(item.key, undefined, item);
4848
}
4949
}
50+
} else if (blocks.inverse.yield) {
51+
blocks.inverse.yield();
52+
}
5053
});
5154

5255
}
@@ -490,6 +493,27 @@ test("MorphLists in childNodes are properly cleared", function() {
490493
strictEqual(destroyedRenderNodeCount, 6, "cleanup hook was invoked again");
491494
});
492495

496+
test("MorphLists in each else are properly cleared", function() {
497+
var template = compile(`{{#each items as |item|}}<div>{{item.name}}</div>{{else}}<div>{{noItems}}</div>{{/each}}`);
498+
499+
let a1 = { key: "a", name: "A1" };
500+
let a2 = { key: "a", name: "A2" };
501+
var object = {items: [], noItems: "items not found"};
502+
503+
var result = template.render(object , env);
504+
equalTokens(result.fragment, `<div>items not found</div>`);
505+
506+
object.items.push(a1);
507+
object.items.push(a2);
508+
result.rerender(env, object);
509+
510+
equalTokens(result.fragment, "<div>A1</div><div>A2</div>");
511+
strictEqual(destroyedRenderNodeCount, 1, "cleanup hook was invoked for else morph");
512+
513+
result.rerender(env, { items: [] });
514+
strictEqual(destroyedRenderNodeCount, 5, "cleanup hook was invoked for each morph");
515+
});
516+
493517
test("Pruned render nodes invoke a cleanup hook when cleared", function() {
494518
var object = { condition: true, value: 'hello world' };
495519
var template = compile('<div>{{#if condition}}<p>{{value}}</p>{{/if}}</div>');

packages/htmlbars-runtime/lib/hooks.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createChildMorph } from "./render";
44
import { keyLength, shallowCopy } from "../htmlbars-util/object-utils";
55
import { validateChildMorphs } from "../htmlbars-util/morph-utils";
66
import { RenderState, clearMorph, clearMorphList, renderAndCleanup } from "../htmlbars-util/template-utils";
7+
import { visitChildren } from "../htmlbars-util/morph-utils";
78
import { linkParams } from "../htmlbars-util/morph-utils";
89

910
/**
@@ -258,6 +259,11 @@ function yieldItem(template, env, parentScope, morph, renderState, visitor) {
258259
handledMorphs[foundMorph.key] = foundMorph;
259260
yieldTemplate(template, env, parentScope, foundMorph, renderState, visitor)(blockArguments, self);
260261
} else {
262+
if(morph.childNodes){
263+
visitChildren(morph.childNodes, function (node) {
264+
clearMorph(node, env, true);
265+
});
266+
}
261267
var childMorph = createChildMorph(env.dom, morph);
262268
childMorph.key = key;
263269
morphMap[key] = handledMorphs[key] = childMorph;

0 commit comments

Comments
 (0)