Passing classes / components with properties in Coldfusion

I am new to CF based on the .NET background. I am wondering what kind of best practice thing will be for the next situation.

Say I have a component,, car.cfcand I have a function inside this component that requires properties:

<cfcomponent>
    <cfproperty name="Name" />
    <cfproperty name="Model" />
    <cfproperty name="Make" />

    <cffunction name="BuildCarXML">
        <cfargument name="car" type="car" />
        <cfsavecontent variable="xmlCar">
            <?xml version="1.0" encoding="UTF-8" ?>
            <car>
               <name>#arguments.car.Name#</name>
            </car>
        </cfsavecontent>
        <cfreturn xmlCar />
    </cffunction>
</cfcomponent>

Finally, I call this function from the cfm page:

<cfscript>
    cfcCar = CreateObject("car");
    cfcCar.Name="AU";
</cfscript>
<cfdump var="#cfcCar.BuildCarXML(cfcCar)#">

My question is, is this the right / best way to do this?

+4
source share
1 answer
<cfcomponent accessor="true">
    <cfproperty name="name" />
    <cfproperty name="model" />
    <cfproperty name="make" />

    <cffunction name="BuildCarXML" output="false">
        <cfsavecontent variable="local.xmlCar">
            <cfoutput><?xml version="1.0" encoding="UTF-8" ?>
            <car>
               <name>#variables.name#</name>
            </car></cfoutput>
        </cfsavecontent>
        <cfreturn xmlCar />
    </cffunction>
</cfcomponent>

Finally, we call this function from the cfm page:

<cfscript>
    cfcCar = new Car();
    cfcCar.setName("AU");
    writeDump(cfcCar.BuildCarXML());
</cfscript>
+1
source

Source: https://habr.com/ru/post/1525721/


All Articles