Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Phlex::Bucket
allows you to define a bucket of components which can then be included into your views and other components.Here’s how it works:
First, you can define a module to hold your components and have it extend
Phlex::Bucket
. Then define as many components in this module as you like. These should be namespaced directly under your module.Then include that module in your base component/view classes.
Now you can use components from this bucket by calling their name as a method.
The only real downside to this is all your components need to live in a single namespace. That seems to work okay for React and web components, so maybe it’ll work for you. Of course you can continue to render namespaced components by passing them to
render
.One gotcha to look out for is if you have a component that takes no arguments, you’ll need to include empty parentheses to tell Ruby this is a method call, not a constant lookup.
You can create and import as many “buckets” as you like, but they can’t have conflicting names, since they all share a single namespace once the methods are included.
Why can't we do
MyNamespace::Card()
?Because that would be calling
Card()
onMyNamespace
, which has no idea which component you’re currently rendering. The only way to support this would be to have a global variable keep track of the currently rendering component, which would be really nasty. When you callCard()
onself
, it’s easy to pass the rendering context to the new component.Update: If you want to know how this could be done ^^ here's a rough sketch. f8cca44