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

Allow GJS/GTS route templates #20768

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports = {

'disable-features/disable-async-await': 'error',
'disable-features/disable-generator-functions': 'error',
'import/no-unresolved': [
'error',
{
ignore: ['@ember/template-compiler'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a build-time feature that we added in RFC 931 but haven't used internally in ember-source yet. The whole implementation of it lives in babel-plugin-ember-template-compilation.

},
],
},

settings: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface OutletDefinitionState {
template: Template;
controller: unknown;
model: unknown;
component: object | undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The choice of type here reflects that a component is anything with a component manager, and having a component manager requires being a weakmap key.

}

const CAPABILITIES: InternalComponentCapabilities = {
Expand Down
4 changes: 4 additions & 0 deletions packages/@ember/-internals/glimmer/lib/syntax/outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export const outletHelper = internalHelper(
return model;
});

let componentRef = childRefFromParts(outletRef, ['render', 'component']);
named['component'] = componentRef;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike model above, this argument doesn't change for the lifetime of this render state. Its lifetime is the same as outletRef.render.template, rather than outletRef.render.model.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it doesn't change then something like createConstRef(state.component, '@component') should do it. It'll result in less bounds tracking for the DOM etc


if (DEBUG) {
named['model'] = createDebugAliasRef!('@model', named['model']);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not really sure what's up with this, does this just predate createComputeRef taking an optional label argument? (In any case the positioning of the new code split these two related chunks)

Expand Down Expand Up @@ -160,6 +163,7 @@ function stateFor(
name: render.name,
template,
controller: render.controller,
component: render.component,
model: render.model,
};
}
Expand Down
6 changes: 6 additions & 0 deletions packages/@ember/-internals/glimmer/lib/utils/outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export interface RenderState {
*/
model: unknown;

/**
* When routing directly to a component (as opposed to a bare template), this
* stores the component.
*/
component: object | undefined;

/**
* template (the layout of the outlet component)
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/@ember/-internals/glimmer/lib/views/outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default class OutletView {
name: TOP_LEVEL_NAME,
controller: undefined,
model: undefined,
component: undefined,
template,
},
};
Expand All @@ -92,6 +93,7 @@ export default class OutletView {
name: TOP_LEVEL_NAME,
template,
controller: undefined,
component: undefined,
model: undefined,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ if (ENV._DEBUG_RENDER_TREE) {
this.outlet({
type: 'route-template',
name: 'index',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: this.controllerFor('index'),
template: 'my-app/templates/index.hbs',
bounds: this.elementBounds(this.element!),
Expand All @@ -103,15 +103,15 @@ if (ENV._DEBUG_RENDER_TREE) {
this.outlet({
type: 'route-template',
name: 'foo',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: this.controllerFor('foo'),
template: 'my-app/templates/foo.hbs',
bounds: this.elementBounds(this.element!),
children: [
this.outlet({
type: 'route-template',
name: 'foo.index',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: this.controllerFor('foo.index'),
template: 'my-app/templates/foo/index.hbs',
bounds: this.nodeBounds(this.element!.lastChild),
Expand All @@ -127,15 +127,15 @@ if (ENV._DEBUG_RENDER_TREE) {
this.outlet({
type: 'route-template',
name: 'foo',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: this.controllerFor('foo'),
template: 'my-app/templates/foo.hbs',
bounds: this.elementBounds(this.element!),
children: [
this.outlet({
type: 'route-template',
name: 'foo.inner',
args: { positional: [], named: { model: 'wow' } },
args: { positional: [], named: { model: 'wow', component: undefined } },
instance: this.controllerFor('foo.inner'),
template: 'my-app/templates/foo/inner.hbs',
bounds: this.nodeBounds(this.element!.lastChild),
Expand All @@ -151,15 +151,15 @@ if (ENV._DEBUG_RENDER_TREE) {
this.outlet({
type: 'route-template',
name: 'foo',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: this.controllerFor('foo'),
template: 'my-app/templates/foo.hbs',
bounds: this.elementBounds(this.element!),
children: [
this.outlet({
type: 'route-template',
name: 'foo.inner',
args: { positional: [], named: { model: 'zomg' } },
args: { positional: [], named: { model: 'zomg', component: undefined } },
instance: this.controllerFor('foo.inner'),
template: 'my-app/templates/foo/inner.hbs',
bounds: this.nodeBounds(this.element!.lastChild),
Expand Down Expand Up @@ -643,7 +643,7 @@ if (ENV._DEBUG_RENDER_TREE) {
this.outlet({
type: 'route-template',
name: 'index',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: this.controllerFor('index'),
template: 'my-app/templates/index.hbs',
bounds: this.elementBounds(this.element!),
Expand All @@ -665,15 +665,15 @@ if (ENV._DEBUG_RENDER_TREE) {
{
type: 'route-template',
name: 'application',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: instance!.lookup('controller:application'),
template: 'foo/templates/application.hbs',
bounds: this.elementBounds(this.element!),
children: [
this.outlet({
type: 'route-template',
name: 'index',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: instance!.lookup('controller:index'),
template: 'foo/templates/index.hbs',
bounds: this.nodeBounds(this.element!.firstChild),
Expand Down Expand Up @@ -703,15 +703,15 @@ if (ENV._DEBUG_RENDER_TREE) {
{
type: 'route-template',
name: 'application',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: instance!.lookup('controller:application'),
template: 'foo/templates/application.hbs',
bounds: this.elementBounds(this.element!),
children: [
this.outlet({
type: 'route-template',
name: 'index',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: instance!.lookup('controller:index'),
template: 'foo/templates/index.hbs',
bounds: this.nodeBounds(this.element!.firstChild),
Expand Down Expand Up @@ -750,15 +750,15 @@ if (ENV._DEBUG_RENDER_TREE) {
{
type: 'route-template',
name: 'application',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: instance!.lookup('controller:application'),
template: 'foo/templates/application.hbs',
bounds: this.elementBounds(this.element!),
children: [
this.outlet({
type: 'route-template',
name: 'index',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: instance!.lookup('controller:index'),
template: 'foo/templates/index.hbs',
bounds: this.nodeBounds(this.element!.firstChild),
Expand All @@ -776,7 +776,7 @@ if (ENV._DEBUG_RENDER_TREE) {
this.outlet({
type: 'route-template',
name: 'index',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: this.controllerFor('index'),
template: 'my-app/templates/index.hbs',
bounds: this.elementBounds(this.element!),
Expand Down Expand Up @@ -1639,7 +1639,7 @@ if (ENV._DEBUG_RENDER_TREE) {
this.outlet({
type: 'route-template',
name: 'application',
args: { positional: [], named: { model: undefined } },
args: { positional: [], named: { model: undefined, component: undefined } },
instance: this.controllerFor('application'),
template: this.owner.hasRegistration('template:application')
? 'my-app/templates/application.hbs'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { tracked } from '@ember/-internals/metal';
import { set } from '@ember/object';
import { backtrackingMessageFor } from '../../utils/debug-stack';
import { runTask } from '../../../../../../internal-test-helpers/lib/run';
import { template } from '@ember/template-compiler';

moduleFor(
'Application test: rendering',
Expand Down Expand Up @@ -553,5 +554,88 @@ moduleFor(
this.assertText('first');
});
}

async ['@test it can use a component as the route template'](assert) {
this.router.map(function () {
this.route('example');
});

this.add(
'route:example',
Route.extend({
model() {
return {
message: 'I am the model',
};
},
})
);

this.add(
'controller:example',
class extends Controller {
message = 'I am the controller';
}
);

this.add(
'template:example',
class extends Component {
message = 'I am the component';

static {
template(
`<div data-test="model">{{@model.message}}</div>
<div data-test="controller">{{@controller.message}}</div>
<div data-test="component">{{this.message}}</div>`,
{
component: this,
}
);
}
}
);

await this.visit('/example');

assert.strictEqual(this.$('[data-test="model"]').nodes[0]?.innerText, 'I am the model');
assert.strictEqual(
this.$('[data-test="controller"]').nodes[0]?.innerText,
'I am the controller'
);
assert.strictEqual(
this.$('[data-test="component"]').nodes[0]?.innerText,
'I am the component'
);
}

async ['@test can switch between component-defined routes']() {
this.router.map(function () {
this.route('first');
this.route('second');
});
this.add('template:first', template('First'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This template is the JS version of our <template></template>, so the value here is a component.

this.add('template:second', template('Second'));
await this.visit('/first');
this.assertText('First');
await this.visit('/second');
this.assertText('Second');
}

async ['@test can switch from component-defined route to template-defined route']() {
this.router.map(function () {
this.route('first');
this.route('second');
});
this.add('template:first', template('First'));
this.addTemplate(
'second',
'Second sees {{#if @component}}A Component{{else}}No Component{{/if}}'
);
await this.visit('/first');
this.assertText('First');
await this.visit('/second');
this.assertText('Second sees No Component');
}
}
);
26 changes: 25 additions & 1 deletion packages/@ember/routing/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
prefixRouteNameArg,
stashParamNames,
} from './lib/utils';
import { hasInternalComponentManager } from '@glimmer/manager';

export interface ExtendedInternalRouteInfo<R extends Route> extends InternalRouteInfo<R> {
_names?: unknown[];
Expand Down Expand Up @@ -1825,6 +1826,15 @@ export function getRenderState(route: Route): RenderState | undefined {
return route[RENDER_STATE];
}

import { precompileTemplate } from '@ember/template-compilation';

const RoutableComponent = precompileTemplate(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to name it this for the LOLs.

`<@component @model={{@model}} @controller={{this}} />`,
{
strictMode: true,
}
);

function buildRenderState(route: Route): RenderState {
let owner = getOwner(route);
assert('Route is unexpectedly missing an owner', owner);
Expand All @@ -1836,17 +1846,31 @@ function buildRenderState(route: Route): RenderState {

let model = route.currentModel;

let template = owner.lookup(`template:${route.templateName || name}`) as
let templateOrComponent = owner.lookup(`template:${route.templateName || name}`) as
| TemplateFactory
| object
| undefined;

let template: TemplateFactory | undefined;
let component: object | undefined;

if (templateOrComponent) {
if (hasInternalComponentManager(templateOrComponent)) {
template = RoutableComponent;
component = templateOrComponent;
} else {
template = templateOrComponent as TemplateFactory;
}
}

let render: RenderState = {
owner,
into: undefined,
outlet: 'main',
name,
controller,
model,
component,
template: template?.(owner) ?? route._topLevelViewTemplate(owner),
};

Expand Down
Loading