On 5/24/05, Pete Ruckelshaus <[EMAIL PROTECTED]> wrote:
> I'm using XMLhttp to do some RIA stuff.  Rather than having a separate
> CFM file for each XML file that I need to return, I want to have a
> single CFC with a bunch of methods in it that return the proper
> content (XML, using cfheader/cfcontent) to the app.
> 
> The question is, can I do this (and is it good practice)?  I have a
> script that I'm working on but it's not working; a cfm page with
> similar code on it works just fine.  Here is the code:
> 

<snip>

> Are there any glaring errors in this code that would explain why I'm
> not getting anything but whitespace back?  Also, I'm calling the CFC
> like this 
> http://localhost/cfc/xmlconduit.cfc?method=getcontactsbyorganization&param=5
> 

Yeah, there's lots of things going on in there that shouldn't be. For
starters, you specify a returnType of XML, yet there's no cfreturn in
the function, so nothing comes back. Second, you have a bunch of
scoping (or lack thereof) issues. Third, don't use cfsetting and
cfcontent and those tags within CFCs -- they can cause problems, and
I'm not even sure that cfcontent works, quite honestly. Fourth, I
would argue that it's a bad practice to directly refer to your calling
environment's request scope. I would put in an init function and pass
the datasource in there and place it in an instance variable (See code
below). Last, your "param" argument says it's required, yet you
provide a default value of 0. Don't do that. Either it's required or
it's not. If it's not, then use a default.

Here's a cleaned up version of your code. Your best bet is to cut and
paste it into your editor because I kept in tabs and line breaks. I
didn't test the code because I don't have your datasource, but it
should be close enough.

<cfcomponent name="XMLconduit" author="Pete Ruckelshaus"
displayname="XMLconduit" hint="This .cfc is used for all XML-returned
data for the application.">
        <cffunction name="init" access="public" returntype="XMLconduit"
output="false" hint="Constuctor for setting the datasource name.">
                <cfargument name="datasource" type="string" required="false"
hint="The database to connect to." />
                
                <cfset variables.instance = structNew() />
                <cfset variables.instance["datasource"] = arguments.datasource 
/>
                
                <cfreturn this />
        </cffunction>
        
        <cffunction name="getContactsByOrganization" access="public"
output="Yes" returntype="XML">
                <cfargument name="param" required="Yes" type="numeric">
                <cfset var getContacts = "" />
                <cfset var xml = "" />

                <cfquery name="getContacts" 
datasource="#variables.instance['datasource']#">
                SELECT
                        C.ID,
                        C.fname,
                        C.lname
                FROM
                        tblContacts C
                WHERE
                        C.contactTypeID = (SELECT id FROM tblContactTypes WHERE 
typename =
'individual') AND C.organizationid = <cfqueryparam
value="#arguments.param#" cfsqltype="cf_sql_integer" null="false" />
                ORDER BY
                        C.fname,
                        C.lname
                </cfquery>
                
                <cfxml variable="xml">
                        <contactdata>
                                <cfloop query="getContacts">
                                        <id>#getContacts.id#</id>
                                        
<firstname>#getContacts.fname#</firstname>
                                        <lastname>#getContacts.lname#</lastname>
                                </cfloop>
                        </contactdata>
                </cfxml>
                
                <cfreturn xml />
        </cffunction>
</cfcomponent>

Your calling code would then be something like:

<cfset variables.objXMLConduit = createObject("component",
"XMLconduit").init(datasource:request.app.dsn) />

<cfoutput>#variables.objXMLConduit.getContactsByOrganization(param:myValue)#</cfoutput>

Something very close to the above should get you going.

Hope this helps?

Regards,
Dave.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking 
application. Start tracking and documenting hours spent on a project or with a 
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:207553
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to