cffunction Access =

Phi*_*enn 4 coldfusion constructor function

如果我使用access =“ remote”将cfselect绑定到cfc,那么我将失去拥有Init()构造函数的能力。

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

实例化组件时,我习惯于将数据源名称传递给Init函数,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

Ada*_*tle 5

Phillip,在这种情况下,我通常要做的是:

  1. 在onApplicationStart中创建对象,并将其存储到Application范围。在这里,您将使用数据源的其他设置进行初始化。
  2. 创建一个基本上是实物存根的远程代理CFC,然后将选择字段绑定到该CFC。

onApplicationStart:

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

和远程代理CFC:

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

请注意,为简洁起见,我遗漏了许多最佳实践(即,指定参数类型,必需的等)……因此,请勿仅复制并粘贴此示例。我只是想举例说明这个想法。