-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
{{else}} block for {{component}} helper #12250
Conversation
ec74098
to
40edd67
Compare
``` | ||
|
||
Note: When the name of the component is `null` or `undefined`, nothing is rendered. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works without the 'Note:' prefix, can you remove the prefix?
This looks great, I definitely think this is a nice addition. Since this adds new API, can you please add a feature flag (perhaps named 'ember-htmlbars-component-else') for the new behavior? |
Can you also add a test showing that starting with the else block rendered can leave the else block state when a component can be found (there is already a test for the other way around)? |
|
||
// If the value passed to the {{component}} helper is undefined or null, | ||
// don't create a new ComponentNode. | ||
if (componentPath === undefined || componentPath === null) { | ||
return; | ||
} | ||
|
||
env.hooks.component(morph, env, scope, componentPath, params, hash, { default: template, inverse }, visitor); | ||
const result = lookupComponent(env.container, componentPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can avoid this temp variable via destructuring:
const {
component,
layout
} = lookupComponent(..........);
I did think of one notable difference here: the helper now does a container lookup for every render/rerender. I am not sure that is an issue at all, but I did want to point it out. We might be able to mitigate by moving some of the work to setupState instead of render... |
The {{#foo-bar}}{{else}}If the foo-bar component doesn't exist show this instead!{{/foo-bar}} |
You could instead implement the behavior of this PR with a helper. {{#if (is-a-real-component type)}}
{{component type ...}}
{{else}}
Invalid component type: {{type}}
{{/if}} |
40edd67
to
db0de0c
Compare
Updated docs Feature flagged
db0de0c
to
2b034b9
Compare
@mmun - There is no public API for determining if a component is available for a given string ( |
@rwjblue So let's fix that instead. |
@mmun - Meh. It seems reasonable for |
Simple. |
@mmun - OK, that is a wonderful reason!! Why didn't you say that? 😜 @xcambar - Merging this, would actually break the ability of the component itself to render an inverse block (and be a SemVer violation since this does work properly today (see JSBin demo)). We need tests to confirm that |
@rwjblue am bad at words n stuff |
👍 I didn't even know about Before it's too late to ask, isn't it kind of semantically weird, this |
Rephrasing: "Can you help me understand the use case for |
@xcambar It allows things like |
@xcambar In my opinion, you're totally right that it's a bit weird. It happened organically to solve issues at the time (like the ones @rwjblue listed). A more general solution is being considered (code name: "named blocks", but it's still in the brainstorming stage). The tl;dr is that |
Thanks. I have a much better understanding now. Fel free to close the PR. 👍 to @mmun for having more APIs public to allow the originally proposed behaviour, then. |
FYI, I've just published ember-cli-if-component that has all the required features (hopefully 😉 ). I'll be very happy to have your feedback on it. |
Answering my own needs (see http://discuss.emberjs.com/t/failsafe-component-helper/8705),
I added the
{{else}}
block for the{{component}}
helper; this block is rendered when Ember is not able to lookup the component.This solves a different use case that when
null
orundefined
is given to the helper as a component name. It allows much simpler CPs to generate component names (without component lookups) yet with a failsafe display.The updated docs will certainly explain it all much better :)
I will also take the opportunity to discuss about the values
null
orundefined
.I didn't change the existing behaviour but I'm inclined to think it would fit better in the
{{else}}
block (giving responsibility to the developers) than in the internals of the helper. What do you think?