4 Replies Latest reply on Apr 8, 2013 10:08 AM by jfb00

    cfcomponent extends question

    jfb00 Level 3

      Hi all,

      i have two cfc' components usind extends as:

      <!--- myParent.cfc --->

      <cfcomponent displayname="myParent" output="false">

          <cfscript>

               this.myData = '';

          </cfscript>

       

          <cffunction access="remote" name="setQueryData" output="false" >

               <cfquery name="this.myData" datasource="#myDatasource#">

                  SELECT *

                  FROM myTable

              </cfquery>

          </cffunction>

      </cfcomponent>

       

      <!--- myExtend.cfc --->

      <cfcomponent extends="myParent">

           <cffunction access="remote" name="getData" output="false" >

               <cfquery name="qryData" datasource="#myDatasource#">

                  SELECT *

                  FROM this.myData

              </cfquery>

       

           <cfreturn qryData />

          </cffunction>

      </cfcomponent>

       

      <!--- myTest.cfm --->

      <cfset myObj = CreateObject("component", "cfcs.myParent") />

      <cfset myObj.setQueryData() />

      <cfset mySecondObj = CreateObject("component", "cfcs.myChild") />

      <cfset finalData = mySecondObj.getData() />

      <cfdump var="#finalData#">

       

      this.myData is available in the child cfc component but it doesn't show any data.

      Can I access data between components?

      Thanks

        • 1. Re: cfcomponent extends question
          Dan Bracuk Level 5

          I'm surprised you didn't get an error running that code.  It looks like you are trying to do a query of queries, but you have a datasource attribute in your query tag.

           

          As far as your question goes, your myObj and mySecondObj objects are independent.  To do what you are attempting, you have to run the setQueryData function from the child component.

           

          Your scopes might also be incorrect.  I never do this sort of thing in ColdFusion so I can't be sure.  In other programming languages, you use the word base, or parent in the child classes.  I could look up the correct scope for ColdFusion, but so could you.

          • 2. Re: cfcomponent extends question
            jfb00 Level 3

            Hi Dan,

            I try to create a sample code from original code, i didn't test the code but i will.

            The issue is that my cfc file is huge and my flex text editor is crashing, so i want to keep going with a new cfc file, i dont want to call the data again becuase affect my page performance.

            How can I extend my cfc file and use the same data?

            Thanks for you reply and help!

            • 3. Re: cfcomponent extends question
              Dan Bracuk Level 5

              Caching your query might be appropriate for this situation.

              • 4. Re: cfcomponent extends question
                jfb00 Level 3

                I got it Dan,

                In my parent component i need to declate the extend name as:

                <cfcomponent displayname="myParent" output="false" extends="myExtend" >

                Now I can access to all with:

                <cfset finalData = myObj.getData() />

                Also I am using variables instead of this. Thiscan be use to make a call outside the scope.

                Best,