Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ moduleFor(
this.assertStableRerender();
}

async '@test Can use `this` from explicit scope'() {
await this.renderComponentModule(() => {
let state = { cls: 'Hello, world!' };

return template('<div>{{this.cls}}</div>', {
scope: () => ({ this: state }),
});
});

this.assertHTML('<div>Hello, world!</div>');
this.assertStableRerender();
}

async '@test Can use inline if and unless in strict mode templates'() {
await this.renderComponentModule(() => {
return template('{{if true "foo" "bar"}}{{unless true "foo" "bar"}}');
Expand Down
20 changes: 17 additions & 3 deletions packages/@ember/template-compiler/lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,24 @@ function buildEvaluator(options: Partial<EmberPrecompileOptions> | undefined) {
}

return (source: string) => {
const argNames = Object.keys(scope);
const argValues = Object.values(scope);
let hasThis = Object.prototype.hasOwnProperty.call(scope, 'this');
let thisValue = hasThis ? (scope as { this?: unknown }).this : undefined;

return new Function(...argNames, `return (${source})`)(...argValues);
let argNames: string[] = [];
let argValues: unknown[] = [];

for (let [name, value] of Object.entries(scope)) {
if (name === 'this') {
continue;
}

argNames.push(name);
argValues.push(value);
}

let fn = new Function(...argNames, `return (${source})`);

return hasThis ? fn.call(thisValue, ...argValues) : fn(...argValues);
};
}
}