Trying to call a subcomponent from within its main...
# lucee
a
Trying to call a subcomponent from within its main component in Lucee 6. Here is the code
Copy code
//primary.cfc
component {
   function init(){
      variables.sub = new $sub(); //new this$sub(); //new primary$sub();
      return this;
   }
}
component name="sub" {
   function init(){ return this; }
}
r
One component per file. Move your "sub" component to
sub.cfc
then
variables.sub = new sub()
Apologies, it looks like it is possible with Lucee 6
a
Appreciate the link, but it does not address how to call the sub component from within the main component
πŸ‘ 1
r
Sorry, I took a quick look to see what I could find but came up empty.
a
no worries πŸ˜‰
a
That link is just some chatter about what could be the syntax. The closest I could find to docs is this: https://dev.lucee.org/t/what-is-new-in-lucee-6-part-1/13247#why-use-sub-components-13. I can't find anything on the actual docs site @ https://docs.lucee.org/ @artknight can you get the sub component syntax to work outside of the main CFC? IE: as per what little docs there are in that "what's new?" doc? I was gonna have a play around and work out what the story is, but as per the other thread I just started... there's no stable Lucee 6 docker image it seems, and I can't be arsed dicking around with SNAPSHOTs or BETAs.
🀣 1
πŸ‘ 1
a
@Adam Cameron Yes, invoking the sub component on the outside using the
new component$sub()
works
πŸ‘ 1
a
This works as expected for me:
Copy code
// Parent.cfc
component {
    function init() {
        writeOutput("Main init<br>");
        variables.sub = new Parent$Sub();
    }

    function getSub() {
        return variables.sub;
    }
}

component name="Sub" {
    function init() {
        writeOutput("Sub init<br>");
    }
}
Copy code
<!--- test.cfm --->
<cfscript>
    parent = new Parent()
    sub = parent.getSub()
    writeDump([
        sub=sub,
        metadata = getMetaData(sub)
    ])
</cfscript>
r
They should stick that in the docs.
a
#cue someone from the Lucee project to tell us to go do it ourselves.
I probably would... if there was any existing reference to sub components in the docs at all. I can't be arsed working out how to add new pages etc.
@artknight any feedback on that code?
a
@Adam Cameron The Parent$Sub() syntax worked for me as well. It’s weird because I had tried it before unsuccessfully. However, I still believe that this$Sub() should be the correct way.
a
this
in CFML is a reference to an object. Whereas
Parent$Sub
is a reference to a component. So using
this
in that context doesn't make any sense.
I wonder if when you tried it earlier and it didn't work whether there was a casing mismatch or something? (wild guess).
I mean one cannot go
anotherOne = new this()
from within a CFC to create another instance of the same CFC can it (suddenly I am unsure, ha!)
a
I would argue that this would/should refer to the component you are in, hence this$Sub() would be logical.
I was under the impression that CF is case agnostic, but I wonder if this is not the case with sub components
a
I would argue
Right. So despite the fact I just explained why that is off the mark, you'd still argue it. Cool. Um... file system references will still be case-sensitive on case-sensitive file systems. And last time I checked (which admittedly was ages ago... like over a decade), CFC paths / names are used for the file system access of same. But we don't need to conjecturize here. You have yer code and your computer, so you could... check?
a
@Adam Cameron ok, tested it with lowercase.. all worked as well. I am not sure what I did wrong the first time around..
a
weird. Still: all sorted And now I know that Lucee has sub components. Still thinking "what's an actual valid use case for this?" (disagreeing with all the ones the docs offer as being pretty specious), but at least it gave the the Lucee dev team something to do instead of fixing any of the 100+ _critical_+ bugs they have in already-released features. Which is... a thing. Apparently.
πŸ‘ 2
r
Glad it wasn't just me that didn't understand the use case for it, but I am not always the sharpest crayon in the box.
a
Thank you @Adam Cameron
πŸ‘ 1
d
One use case I can see where sub-components could be useful is when you just need a structured data object and you want something more robust than just a struct, but it's so highly coupled to the main component that it's not usable outside of the component.
a
that it's not usable outside of the component.
Indeed. I await to see how ppl abuse this feature, given the subclasses are accessible from the calling code. I guess the benefit over just "another CFC in the same package" is that the sub has access to the private stuff in the main class.
c
FYI, here's some info on subcomp , time point 25:05

https://youtu.be/BLjneasqBAk?si=At2gJ5g7fNvHTKRt&t=1499β–Ύ

. Their rationale is: improvement in performance by allowing for reduction in the number of separate component files.