Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.25] fix nested partials #732

Merged
merged 1 commit into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/@glimmer/runtime/lib/syntax/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@glimmer/reference';
import {
assert,
assign,
Dict,
dict,
EMPTY_ARRAY,
Expand Down Expand Up @@ -259,14 +260,18 @@ export class PartialInvoker implements DynamicInvoker<ProgramSymbolTable> {
let partial = unwrap(_partial);
let partialSymbols = partial.symbolTable.symbols;
let outerScope = vm.scope();
let outerEvalScope = outerScope.getEvalScope();
let partialScope = vm.pushRootScope(partialSymbols.length, false);
partialScope.bindCallerScope(outerScope.getCallerScope());
partialScope.bindEvalScope(outerScope.getEvalScope());
partialScope.bindEvalScope(outerEvalScope);
partialScope.bindSelf(outerScope.getSelf());

let { evalInfo, outerSymbols } = this;

let locals = dict<VersionedPathReference<Opaque>>();
let locals = outerScope.getPartialMap();
if (locals === null) {
locals = dict<VersionedPathReference<Opaque>>();
}

for (let i = 0; i < evalInfo.length; i++) {
let slot = evalInfo[i];
Expand All @@ -275,12 +280,16 @@ export class PartialInvoker implements DynamicInvoker<ProgramSymbolTable> {
locals[name] = ref;
}

let evalScope = outerScope.getEvalScope()!;
if (outerEvalScope === null) {
outerEvalScope = {};
}

let mergedScope = assign(outerEvalScope, locals);

for (let i = 0; i < partialSymbols.length; i++) {
let name = partialSymbols[i];
let symbol = i + 1;
let value = evalScope[name];
let value = mergedScope[name];

if (value !== undefined) partialScope.bind(symbol, value);
}
Expand Down
47 changes: 47 additions & 0 deletions packages/@glimmer/runtime/test/partial-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,53 @@ QUnit.test('nested dynamic partial with dynamic content', () => {
equalTokens(root, `Before <div>Testing wat are <div>Nested you doing?</div></div> After`);
});

QUnit.test('nested partials within nested `{{#with}}` blocks', () => {
let template = compile(`Hi {{person1}}. {{#with 'Sophie' as |person1|}}Hi {{person1}} (aged {{age}}), {{person2}}, {{person3}} and {{person4}}. {{partial 'person2-partial'}}{{/with}}`);

env.registerPartial('person2-partial', `{{#with 'Ben' as |person2|}}Hi {{person1}} (aged {{age}}), {{person2}}, {{person3}} and {{person4}}. {{partial 'person3-partial'}}{{/with}}`);
env.registerPartial('person3-partial', `{{#with 'Alex' as |person3|}}Hi {{person1}} (aged {{age}}), {{person2}}, {{person3}} and {{person4}}. {{partial 'person4-partial'}}{{/with}}`);
env.registerPartial('person4-partial', `{{#with 'Sarah' as |person4|}}Hi {{person1}} (aged {{age}}), {{person2}}, {{person3}} and {{person4}}.{{/with}}`);

render(template, {
person1: 'Context1',
person2: 'Context2',
person3: 'Context3',
person4: 'Context4',
age: 0
});

equalTokens(root, `Hi Context1. Hi Sophie (aged 0), Context2, Context3 and Context4. Hi Sophie (aged 0), Ben, Context3 and Context4. Hi Sophie (aged 0), Ben, Alex and Context4. Hi Sophie (aged 0), Ben, Alex and Sarah.`);

rerender({person1: 'Context1',
person2: 'Context2',
person3: 'Context3',
person4: 'Context4',
age: 0
}, { assertStable: true });

equalTokens(root, `Hi Context1. Hi Sophie (aged 0), Context2, Context3 and Context4. Hi Sophie (aged 0), Ben, Context3 and Context4. Hi Sophie (aged 0), Ben, Alex and Context4. Hi Sophie (aged 0), Ben, Alex and Sarah.`);

rerender({
person1: 'UpdatedContext1',
person2: 'UpdatedContext2',
person3: 'UpdatedContext3',
person4: 'UpdatedContext4',
age: 1
});

equalTokens(root, `Hi UpdatedContext1. Hi Sophie (aged 1), UpdatedContext2, UpdatedContext3 and UpdatedContext4. Hi Sophie (aged 1), Ben, UpdatedContext3 and UpdatedContext4. Hi Sophie (aged 1), Ben, Alex and UpdatedContext4. Hi Sophie (aged 1), Ben, Alex and Sarah.`);

rerender({
person1: 'Context1',
person2: 'Context2',
person3: 'Context3',
person4: 'Context4',
age: 0
});

equalTokens(root, `Hi Context1. Hi Sophie (aged 0), Context2, Context3 and Context4. Hi Sophie (aged 0), Ben, Context3 and Context4. Hi Sophie (aged 0), Ben, Alex and Context4. Hi Sophie (aged 0), Ben, Alex and Sarah.`);
});

QUnit.test('dynamic partial with falsy value does not render', () => {
let template = compile(`Before {{partial name}} After`);

Expand Down