Cffunction access =

If I use access = "remote" to bind cfselect to cfc, 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 use passing the data source name to the Init function when instantiating the component, for example:

<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>
+3
source share
4 answers

Phillip, what I usually do in this scenario:

  • Create an object in onApplicationStart and save it in the Applications area. Here you will initialize with other parameters of your data source.
  • Create a remote CFC proxy, which is basically a stub for the real thing, and bind your selection box to this CFC.

onApplicationStart:

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

And the remote CFC proxy:

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

, (, , ..)... . .

+4

?

CFC -, , init().

/, onApplicationStart application.cfc.

+2

, init, , ?

, ... .

+1

What we did: a software development standard was adopted that prohibits any constructor code in our own developed components, except for one (optional) invocation of the init () method. The so-called constructor code (anything inside the cfcomponent tag that is not inside the cffunction tag) will still be run when you instantiate the object and, as a result, before your method is actually called.

<cfcomponent>

  <cfset init() />

  <cffunction name="init">
    <cfset variables.message = "Hello, World" />
    <cfreturn this />
  </cffunction>

  <cffunction name="remoteMethod" access="remote">
    <cfreturn variables.message />
  </cffunction>

</cfcomponent>
+1
source

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


All Articles