Highlighted

easy one regarding scope and cfinvoke

Community Beginner ,
Jul 01, 2014

Copy link to clipboard

Copied

Why does this work?

<cfset projectList = Application.Projects.getProjects(CompanyID=request.User.CompanyID)>

BUT, this throws an error?(Could not find the ColdFusion component or interface Application.Projects)

<cfinvoke component="Application.Projects" method="getProjects" returnvariable="projectList">

<cfinvokeargument name="companyID" value="#request.User.CompanyID#">

</cfinvoke>

I understand that cfinvoke is trying to look for an 'Application' directory, correct?  The Projects.cfc is located within a directory names 'sys'.  Would it be a better idea to use:

<cfinvoke component="sys.Projects" method="getProjects" returnvariable="projectList">

<cfinvokeargument name="companyID" value="#request.User.CompanyID#">

</cfinvoke>

Basically, is the above the same as:

<cfset projectList = Application.Projects.getProjects(CompanyID=request.User.CompanyID)>

Views

199

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

easy one regarding scope and cfinvoke

Community Beginner ,
Jul 01, 2014

Copy link to clipboard

Copied

Why does this work?

<cfset projectList = Application.Projects.getProjects(CompanyID=request.User.CompanyID)>

BUT, this throws an error?(Could not find the ColdFusion component or interface Application.Projects)

<cfinvoke component="Application.Projects" method="getProjects" returnvariable="projectList">

<cfinvokeargument name="companyID" value="#request.User.CompanyID#">

</cfinvoke>

I understand that cfinvoke is trying to look for an 'Application' directory, correct?  The Projects.cfc is located within a directory names 'sys'.  Would it be a better idea to use:

<cfinvoke component="sys.Projects" method="getProjects" returnvariable="projectList">

<cfinvokeargument name="companyID" value="#request.User.CompanyID#">

</cfinvoke>

Basically, is the above the same as:

<cfset projectList = Application.Projects.getProjects(CompanyID=request.User.CompanyID)>

Views

200

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 01, 2014 0
Most Valuable Participant ,
Jul 01, 2014

Copy link to clipboard

Copied

The first statement works, because the Projects object is already created and stored in the Application scope (probably there is a CreateObject() call somewhere in the methods of your Application.cfc that created the object).  So "Application" in "Application.Projects.getProjects()" is not part of a dot-delimited path, like you use in <cfinvoke>.  Its a scope reference.

In your <cfinvoke>, your dot-delimited path would be "sys.Projects", as you indicated in your third paragraph.

The difference between the two is this: the <cfset projectList...> code snippet is calling a method of an already instantiated object that has been stored in the Application scope; the <cfinvoke> snippet is instantiating a new instance of the object, calling the method, and then throwing away the object, and will do this every time the snippet is called.  Therefore, the first approach is likely to provide better performance (the Project object only gets created once).  However, you must be sure that the Project.cfc is written in a thread-safe manner; otherwise, you run the risk of "race conditions" and unexpected return values.

-Carl V.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 01, 2014 1