Default component type resolution
tobil4sk opened this issue · 4 comments
According to the docs:
you can add @:uiComp("name") metadata in order to customize the component name
Components are resolved by name by adding Comp to the capitalized component name. For instance will try to load SomethingComp class from local context.
"Customize" implies that @:uiComp("name")
is optional, and only necessary if you want a non-default name for the component. With the default resolution rules, this seems like it should work:
class SomethingComp extends h2d.Object implements h2d.domkit.Object {}
class SampleView extends h2d.Flow implements h2d.domkit.Object {
static var SRC =
<sample-view>
<something />
</sample-view>;
public function new(tile:h2d.Tile,?parent) {
super(parent);
initComponent();
}
}
However, instead, it gives the error:
src/SampleView.hx:5: characters 14-23 : SomethingComp does not define component something
It only works if @:uiComp("something")
is added to SomethingComp.
Since it doesn't seem possible for the component name to be anything other than "something"
(see #24), it really makes it hard to understand why this metadata is necessary at all.
You're right that @:uiComp is mostly optional now. But it might still be used I think since you can customize the way the components names are resolved into classes by using domkit.Macros.registerComponentsPath at --init time
What I don't understand here is why there is an error in the sample above. The documentation states: "Components are resolved by name by adding Comp to the capitalized component name". The xml tag is <something />
, and so according to this rule, by default it should look for SomethingComp
. The class exists and from the error message, it clearly was able to locate the correct class: SomethingComp does not define component something
.
Even if the component is in components.SomethingComp
, and that path is registered using components.$Comp
, it still fails unless SomethingComp is marked with @:uiComp("something")
. If the path components.$Comp
is registered, why does components.SomethingComp
need to be marked explicitly as "something"
if that's the class to which <something />
is meant to resolve to anyway, given it's a registered path to look for components in?
Yeah that's something I didn't deal with. In our games now we're using ui.comp.$ as path so we have exact ClassName == comp-name. I guess when there's no @:uiComp we should try to reverse the class resolution to see if we can find a valid component name before using directly the class name.
Hm, I see. I guess for now to avoid confusion it would be best to rewrite the documentation to suggest a similar setup (i.e. custom registration path with matching class and component names). I tried to have a look at the macros myself, and it seems to me like it would be difficult to change this behaviour without completely rewriting the component resolution system.
If the the class name is kept the same as the component name, i.e. "Something", (in the same package and without registerComponentsPath) gives a slightly different error: Could not load component 'something'
.
I added a trace(RESOLVED_COMPONENTS);
right before this line:
Line 69 in 2b12cb9
It looks like the "something" component gets resolved, beause right before the error, it traces the
RESOLVED_COMPONENTS
map which contains something => {c: domkit.MetaComponent, path: null}
. However, the build still fails with the Could not load component 'something'
error. Maybe something to do with the null
path?
Using the macro debugger, I've discovered that loadComponent("something", ...)
gets called twice. The first time, it fails to load the component, and the second time it succeeds. So, it looks like the error comes from the first time the function is called.