Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cffunction Access=

If I use access="remote" for binding a cfselect to a cfc, then I lose the ability to have an Init() constructor.

<cfselect name="CityID" bind="cfc:Components.City.View1({StateID})" value="CityID" display="CityName" bindonload="yes" />

I'm used to passing the datasource name to the Init function when I instantiate a component, like so:

<cfcomponent>
<cffunction name="Init">
<cfargument name="DS">

<cfset Variables.Instance.DS = arguments.DS>
<cfreturn This>
</cffunction>

<cffunction name="View1">
<cfset var qry = "">

<cfquery name="qry" datasource="#Variables.Instance.DS.Datasource#">
SELECT *
FROM Table
</cfquery>
<cfreturn qry>
</cffunction>
</cfcomponent>
like image 656
Phillip Senn Avatar asked Jan 23 '23 14:01

Phillip Senn


2 Answers

Phillip, what I usually do in this scenario is:

  1. Create the object in onApplicationStart, and store it to the Application scope. This is where you will initialize with your datasource other settings.
  2. Create a remote-proxy CFC that is basically a stub for the real thing, and bind your select field to that CFC.

onApplicationStart:

<cffunction name="onApplicationStart">
  <cfset application.dsn = "myDSN" />
  <cfset application.cityFinder = createObject("component", "Components.City").init(application.dsn) />
</cffunction>

And the remote proxy CFC:

<cfcomponent displayName="CityFinderProxy">
  <cffunction name="View1">
    <cfargument name="StateId" />
    <cfreturn application.cityFinder.View1(argumentCollection=arguments) />
  </cffunction>
</cfcomponent>

Note that I've left out a lot of best-practices (i.e. specifying argument types, required, etc) for brevity... so don't just copy and paste this example. I just wanted to illustrate the idea.

like image 66
Adam Tuttle Avatar answered Jan 30 '23 00:01

Adam Tuttle


What is the question, exactly?

Setting a CFC to remote is basically making it a web service, so I guess that is why you wouldn't have the init() constructor.

You could easily set the datasource name in an application/session variable that created in the onApplicationStart portion of your application.cfc file.

like image 38
Jason Avatar answered Jan 30 '23 00:01

Jason