Solved

Using application.cfc with cfajaxproxy

Posted on 2010-08-17
6
824 Views
Last Modified: 2013-12-24
I've just migrated from using application.cfm to application.cfc, for now all i've really done is putting the contents of my application.cfm into the onRequest() function. Everything all fine and dandy except for code which makes ajax calls via cfajaxproxy.

The proxy code has a simple function:

<cffunction name="echo" returnFormat="json" access="remote">
   <cfset var response=StructNew()>
   <cfset response['status']='OK'>
   <cfreturn response>
</cffunction>

The callback code should be able to check for a json object response, however it now receives null in the callbackhandler function's argument. Anyone have any idea of what I can be doing wrong? Again, the only different is that i have replaced application.cfm with application.cfc. Should the onRequest() function return something? It currently has:

<cfinclude template="#arguments.page#">

.. which is for including the target page after my initialisation. Is something missing here, or can anyone suggest anything else i might be doing wrong?

0
Comment
Question by:paddycobbett
  • 4
  • 2
6 Comments
 
LVL 1

Author Comment

by:paddycobbett
Comment Utility
Ok, all I need to know is how to determine the page request type. I found some comments describing the onRequestStart function:

If you are using the OnRequest() method and you expect this application to be used for flash remoting or CFC-based web service calls, this is the ideal time at which to check for the request type (standard page request vs. web service) and if need be, delete the OnRequest() method from the Application.cfc (ex. structDelete(this, "OnRequest")).

So if i can determine if it's a webservice request type then i know to remove the onRequest function. Any idea of how to do this?
0
 
LVL 27

Expert Comment

by:azadisaryev
Comment Utility
instead of doing that, you better migrate to a proper Application.cfc (not just sticking your old code into onRequest() method).  There is really very seldom that one would need the onRequest() method - other methods of Application.cfc are a lot better suited to various functionality.

e.g.
set all your application-wide vars in onApplicationStart() method.
session-level vars will be defined in onSessionStart()
request-specific vars can go onto onRequestStart() method.

You application will run a lot better this way, and you will have more fine-grained control over it. Plus you remote CFCs will start working :)

Azadi
0
 
LVL 1

Author Comment

by:paddycobbett
Comment Utility
Thanks Azadi.. you know I suspected that may be the answer, however when i ported the code to the onRequestStart() method the variables I set via <cfset datasource="mydatasource"> were no longer available in my page via #datasource#. Would i be right in thinking that i should change all the references in my code to #APPLICATION.datasource#, and set this is onApplicationStart(), although there's quite a few variables in many pages I suspect this is probably what i should have done in the first place. Unless you disagree then is definately what i will do. Could i just ask before awarding deserved credit, when is the onSessionStart() function called? I typically create my session objects just after a loggin, so they would be set in the onRequestStart() method. It's not clear to me when the session starts, is it when a new client first hits a page within the application.cfc directory?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Accepted Solution

by:
azadisaryev earned 500 total points
Comment Utility
>> when i ported the code to the onRequestStart() method the variables I set via  <cfset datasource="mydatasource"> were no longer available in my page via #datasource#

in contrast to Application.cfm, which if fully processed before every page request, methods of Application.cfc get executed only at specific times. e.g. onApplicationStart() runs only before the first request to an application page after server restart; onSessionStart() method is executed once per user session when the session is created; onRequestStart() is executed before every page request; etc etc etc

also, since Application.cfc is a CFC, and its methods are functions, the variables you define within them will NOT automatically be available to all pages within your application. if you want a variable to be globally available, you need to define it in one of the persistent scopes (application/session/request) and then refer to it using the scope prefix. like you have already figured out, instead of <cfset datasource="mydatasource"> you would use <cfset application.datasource="mydatasource"> and then refer to the variable in your pages as #application.datasource#.

which scope to define the variable in depends on each variable and how you use it in your application. global vars (like datasource names, etc) which do not change a lot or at all will likely go into application scope. user-specific vars will likely go into session scope. page request-specific vars belong in request scope.

>> when is the onSessionStart() function called?

this method executes once per user session when the - with the first request to an application page from a specific browser. 'user' in session sense is not a person, but a browser (user agent) making a request to a page.
what would you define in onSessionStart() method? some generic session-scope variables, for example to keep track of user's login state, session start time, etc. you can also set/update application-scope variables in there (like increment session counter stored as application-scope var, if you use one).

if you use user authentication in your application and require users to log in, then session scope IS the scope to store user-specific data. like you say, these variables are usually set as session-scope vars in onRequestStart() method after the user logs in.

>> It's not clear to me when the session starts, is it when a new client first hits a page within the application.cfc directory?

yes, exactly. a session is created with the first request from a client (browser) to any page under Application.cfc

hope this helps you.

Azadi
0
 
LVL 1

Author Comment

by:paddycobbett
Comment Utility
Azadi, great, that helped alot, and thanks for taking your time to explain so thoroughly =)

Ok, so that makes sense, the onSessionStart() will likely set up any generic session variables. I wasn't sure how i would set up any session variables specific to a user since i do not have access to their login details in onSessionStart() to be able to associate data for a particular user with data from the database, but what you said makes perfect sense.

So from you say, it's actually possible for a user to start a new session WITHOUT actually successfully logging in. A failed log in attempt for example would still hit a page within the application.cfc directory thereby calling onSessionStart() despite the fact that the application.cfc would likely subsequently forward the user to a login page outside that directory (correct?). With that in mind perhaps it isn't unreasonable to say that even generic session vars shouldn't be set in onSessionStart() but in the onRequestStart() method after a successful login. Surely even session start time should be recorded from when the user actually logs in, if infact he successfully logs in. Do you agree with this? If so i find it hard to justify why sites which require log-in would implement the onSessionStart() method.
0
 
LVL 1

Author Closing Comment

by:paddycobbett
Comment Utility
Thanks alot =)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now