Hi, so i need a suggestion here of how best i can ...
# cfml-general
g
Hi, so i need a suggestion here of how best i can use component: 1. we are using Application.cfm, can't use Application.cfc, its legacy code and no one is willing to change it 2. i can use cfinvoke in few cases but when i have to pass the database name, username, password, it throws error as it does recorgize it even they are declared in Application.cfm, 3. so i have to use init to pass in the details to the component, in this case i have use createobject 4. as its in header file adn it is called everytime, hw can i limit its usage so it called once and stored in an application scope and just do not make object calls again and again as the code is not going to change often, may be in either in request scope or session scope, but i think application is good 5. only issue with application is how to reinitalize it, using application.cfm if needed
a
Which part of that are you having problems with?
g
number 5 spacially and number 4
e
1 . Me to, 2, use DSN, 4, onAPplicationStart you can throw all your code in a file, so it just sits about waiting for action. As for inting, just do something like this: <cfif NOT StructKeyExists(application, "initialized")> <!--- Initialize your database connection here ---> <cfset application.dbConnection = CreateObject("component", "path.to.YourDatabaseComponent").init(dsn, username, password)> <!--- Set a flag to indicate that initialization has been done ---> <cfset application.initialized = true> When you want to kill it, you can set the flag to false.
g
@Evil Ware I can’t use onapplicationstart it’s just not possible in that context
e
What about rewriting your application to pull your template from the database, on items that are mostly if not entirely static, its a no brainer
a
I think @Evil Ware means "when the application starts" more than specifically
onApplicationStart
, which sounds very Application.cfc-ish and they know yer on Application.cfm. But their code - if unhelpfully formatted - kinda explains it. Stick that in yer Application.cfm.
i
@gsr I had similar issue. I stored CFCs in application scope. (more precisely DI1 framework) And set a reload flag (URL variable) so when it’s detected it removes specified CFC from application scope. Does that make sense?
a
If you want to restart yer application, have an endpoint that calls
applicationStop
. The next request will start the application again. Simply "recreating stuff in the application scope" is not the same as restarting the application. Don't expose that end point publicly though, obvs!
e
Yes, I could have explained it far better, but it's Friday, and I am exhausted.
g
like this if (not isdefined('application.objectdeclared')) { my application object } then how will i reinit the application if i need to using application.cfm
g
Is that not what @Evil Ware showed with his answer for Q4 above?
😅 1
i
@gsr to reinit the application use
applicationStop()
as suggested above. Then on the next request (maybe same request if it’s above the condiiton) your condition
if (not isdefined('application.objectdeclared'))
will be true https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-a-b/applicationstop.html
g
When everyone saying applicationstop I have not seen this in application.cfm It’s a part of Applicstion.cfc
a
No it's not. Read the docs for it again.