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

support invoking components with render #1181

Closed

Conversation

NullVoxPopuli
Copy link
Collaborator

@NullVoxPopuli NullVoxPopuli commented Jan 9, 2022

adds support for

await render(
  <template>
  
  </template>
);

or

await render(SomeComponent);

This usage only makes sense if the component takes no args.

alternatively, we could make ember-template-imports not turn the <template> into a component within render, but I feel like that could get complicated when people define the <template> outside of render and then try to pass it render, and then we're back at the same problem.

options?: RenderOptions
): Promise<void> {
let context = getContext();
let template: TemplateFactory;

if (
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

idk if there is a better way to check this

@lifeart
Copy link

lifeart commented Jan 10, 2022

@NullVoxPopuli could we add support for arguments?

await render(SomeComponent, { key: 'value' });

or it should be an side thing?

context.setProperties({ __renderable__: renderable, args, attrs });
const argsForComponent = Object.keys(args).reduce((acc, key) => {
   return `${acc} @${key}={{this.args.${key}}}`;
}, '');
const attrsForComponent = Object.keys(attrs).reduce((acc, key) => {
   return `${acc} ${key}={{this.attrs.${key}}}`;
}, '');
template = hbs`<this.__renderable__  ${argsForComponent} ${attrsForComponent}/>`;

to support reactive mutations on args, https://github.com/lifeart/glimmerx-workshop/blob/master/src/test-helpers/index.ts#L7 may be used.

example: https://github.com/lifeart/glimmerx-workshop/blob/master/src/components/Icon.spec.ts#L12

@NullVoxPopuli
Copy link
Collaborator Author

NullVoxPopuli commented Jan 10, 2022

I think if you want to pass args, you'd do:

let fromTestScope = 2;

await render(
  <template>
    <MyComponent @arg={{2}} @foo={{fromTestScope}} />
  </template)
)

example

That does look like a handy shorthand.
But, 🤷 I'll defer to maintainers.
Personally, i'd want splarguments, so we'd still use the template tags.
It feels like an indirection to do what glimmerx is doing, but it also make sense..
So, i'm on the fence. Happy to add it though if people want

@chriskrycho
Copy link
Contributor

Just checking (without looking 😂) – does this align with the direction suggested by/straight-up implement emberjs/rfcs#785? Such that you could now just pass it scopedTemplate and it would work, assuming that proposal were accepted (or, as you note, <template>)?

@NullVoxPopuli
Copy link
Collaborator Author

This work is a little unrelated to RFC#785, but maybe implemented support by accident?
The RFC says scopedTemplate returns a component, which.. would work 🥳

as is, I've been testing with <template>, and that works (as ember-template-imports always converts lone <template>s to components)

@rwjblue
Copy link
Member

rwjblue commented Jan 24, 2022

This is the same as what is being proposed in emberjs/rfcs#785 (that just adds a way to create a component without also requiring folks use <template></template>).

@rwjblue
Copy link
Member

rwjblue commented Jan 24, 2022

We need to move that RFC forward before proceeding here.

@NullVoxPopuli
Copy link
Collaborator Author

alright 🤷
I think it'd still be handy to:

class Toggle extends Component {
  // ... no args
}

setComponentTemplate(hbs`...`, Toggle);


await render(Toggle);

🥳

Comment on lines +115 to +116
} else {
template = renderable as TemplateFactory;
Copy link
Member

Choose a reason for hiding this comment

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

We can make passing a component work when we aren't under 3.25 as well, the same basic way that ensureSafeComponent works. Something like this:

let randomComponentName = `render-test-helper-${uuid++}`;
owner.register(`component:${randomComponentName}`, renderable);
this.set('__renderable__', randomComponentName);
template = hbs`{{component this.__renderable__}}`;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

do we want or need to though?

@NullVoxPopuli NullVoxPopuli requested a review from rwjblue February 2, 2022 23:14
@rwjblue
Copy link
Member

rwjblue commented May 19, 2022

The capability requested here landed in 2.8.0+ 🎉

@rwjblue rwjblue closed this May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants