Skip to main content
Inspiring
October 16, 2012
Answered

Could someone help me understand component extension?

  • October 16, 2012
  • 2 replies
  • 2684 views

I've read a couple chapters over on component extension (use of the EXTENDS attribute in a CFCOMPONENT tag) but something isn't clicking; so I'm looking to the forums for some supplemental info that'll help me have a eureka moment and comprehend.

As a preface, I have, what I would call, an intermediate knowledge of CF (working towards advanced).  So if we can keep the explanations into as much Layman's terms as possible, that would be appreciated and most helpful.

To my knowledge, if you have a component, you can make its THIS-scope variables and its methods available to other components if those components have an EXTENDS attribute in their CFCOMPONENT tag that uses a fully-qualified dot notation path to the "extended-from" component.

I tried to put this into practice by having a master.cfc and slave.cfc  The master is located off the root in a folder called 'www'.  The slave is located off the root in at the location: 'www/app/coms'.  So in the slave.cfc component I wrote:

<cfcomponent extends="www.master" ...

Assuming that it was the same as CF starting from the root of the website and looking into the www folder and then seeing the master.cfc.

But when I have the slave component call a function that exists in the master, it says it cannot find it.  What am I not getting here?  This sounds like a POWERFUL way to share the application.cfc's THIS-scope variables, so I'd like to comprehend this ability, but there's something I'm missing it seems.

    This topic has been closed for replies.
    Correct answer 12Robots

    Aegis Kleais wrote:

    But we changed it to "/www.application" and boom, it showed both the THIS scope variables set in the 'parent' and the THIS scope variables in the 'child'.

    I'm surprised that works. Don't do it. That is not correct, even if it does work.

    The best way to handle component FQNs is to use mappings. In your example above, create a mapping to /www.

    Jason

    2 replies

    Inspiring
    October 16, 2012

    To your knowledge, is it possible to use a variable for the EXTENDS attribute?

    I have tried using a string variable that used the same path, and I've tried using a variable which was a component object of the master; neither worked.

    Inspiring
    October 16, 2012

    To your knowledge, is it possible to use a variable for the EXTENDS attribute?

    It is not possible the inheritance info is needed at compile time, not at runtime.  So there's no such thing as "variables" at the point at which the info is needed.

    --

    Adam

    12Robots
    Participating Frequently
    October 16, 2012

    Is www part of your FQN?

    When you create an instance of slave.cfc do you use the FQN www.app.coms.slave?

    By the way, more appropriate names are Parent.cfc and Child.cfc. It is not really a master/slave relationship, it is a parent/child relationship. The extended CFC does not control the CFC doing the extending.

    Jason

    Inspiring
    October 16, 2012

    Yeah, the semantics of the naming was just used in reference to one being the "extended from" (master) rather than if one had control over the other or not.

    Actually, I did some local testing and a co-worker suggested that we start the EXTENDS attribute off with a forward slash (for an absolute path) and that fixed it!  All the problem stemmed from me not being familiar with ColdFusion's component searching methods.  In the example of EXTENDS="www.application" if I executed that code from "/www/app/coms" then CF would look for "/www/app/coms/www/application" first, and start traversing up the directory until it could find it.  Which is odd, cause eventually you'd think it would get to the root of the website and go into 'www/application' and find it.

    But we changed it to "/www.application" and boom, it showed both the THIS scope variables set in the 'parent' and the THIS scope variables in the 'child'.

    Just for clarification, too, if both components shared a similar-named function, I could call the parent by using the SUPER scope, right?

    12Robots
    12RobotsCorrect answer
    Participating Frequently
    October 16, 2012

    Aegis Kleais wrote:

    But we changed it to "/www.application" and boom, it showed both the THIS scope variables set in the 'parent' and the THIS scope variables in the 'child'.

    I'm surprised that works. Don't do it. That is not correct, even if it does work.

    The best way to handle component FQNs is to use mappings. In your example above, create a mapping to /www.

    Jason