Results 1 to 4 of 4

Thread: ColdFusion - Application Instantiated, API Caching Stack

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Ext JS Premium Member Gjslick's Avatar
    Join Date
    Feb 2009
    NJ, USA
    Vote Rating

    Default ColdFusion - Application Instantiated, API Caching Stack

    Hey all. This is a bit of a continuation on Aaron Conran's DirectCFM stack. The original stack is located here:

    Added features to the original include:

    1. The ability for the 'Direct' CFC (I called it 'ExtDirect') to be instantiated into the application scope. Upon its instantiation, it runs its CFC introspection routine, and caches the built API script for all subsequent requests for the API.
    2. The ability to use CFC's in sub directories for Ext.Direct.
    3. The ability for Ext.Direct requests to invoke methods on application instantiated CFC instances (if they exist), instead of creating a new instance each time.

    This is an automatic CFC searching implementation, which will recursively search for, and include all of your CFC's with the extDirect="true" attribute in the <cfcomponent> tag, from a configured directory (such as your webroot, or a 'components' folder). It will also only include methods to be used with Ext.Direct that have the extDirect="true" attribute in the <cffunction> tag.


    Quick Start:
    1) Unzip the 3 files into a directory in your in webroot named 'extDirect'.

    2) Add the initialization code into your onApplicationStart() method of your Application.cfc file, with whichever configuration arguments that you would like to customize. All arguments have defaults, and they are shown in the full configuration below:
    <cffunction name="onApplicationStart" output="no">
        <cfset application.ExtDirect = createObject( 'component', 'extDirect.ExtDirect' ).init(
            namespace = '',
            apiDesc = 'APIDesc',
            routerURL = '/extDirect/router.cfm',
            componentDir = '/',   <!--- No trailing slash on any non-webroot directory here. --->
            providerType = 'remoting',
            useApplicationScope = true,
            ignoreCFCParsingErrors = true
    If all of the defaults work for you, the component may be instantiated as such:
    <cffunction name="onApplicationStart" output="no">
      <cfset application.ExtDirect = createObject( 'component', 'extDirect.ExtDirect' ).init()>
    For a full explanation of what each of the arguments does, check the ExtDirect.cfc file's init() method.

    3) Add the extDirect="true" attribute to all components / methods that you want to be used with Ext.Direct. Note that the component tag itself must have extDirect="true" for any methods to be checked and included. Ex:
    <cfcomponent name="Echo" extDirect="true">
      <cffunction name="send" extDirect="true">
        <cfargument name="data" required="true">
        <cfreturn data />
    4) On your page, add the following code after you have included the Ext framework.
    <script type="text/javascript" src="/extDirect/api.cfm"></script>
    <script type="text/javascript">
      Ext.Direct.addProvider( );
    5) You're now ready to go with making any Ext.Direct calls you need. Example for a component named Echo (shown in step 3):
    Code: 'sample', function() {
        // callback here...
    } );
    Note that it doesn't matter which directory that your components are stored in, they are always available directly in the specified JavaScript namespace. So even if your Echo component is really 'components.util.Echo', it is still available in your JavaScript code via '' (or whichever namespace you have chosen).


    1. Unfortunately, all CFC's to be used for Ext.Direct must have a unique name. This is because there are no metaData properties in the Ext.Direct specification, where a directory could be stored.

      So, if you have a component 'com.users.UserManager', you can not also use a component 'com.mySystem.UserManager', as there will be no way to distinguish between the two. Whichever 'UserManager' is found second during the original introspection routine will be the one that is used for Ext.Direct requests.

    Other Notes:

    1. If useApplicationScope = true (the default), the application scope will be checked for an instance of the component by its name, regardless of the directory that the component may be in. So for example, if you wanted to use the component 'com.users.UserManager' which is instantiated in the application scope, it must be instantiated as application.UserManager for it to be used. Otherwise, a new instance of 'com.users.UserManager' will be created, and the requested method will be invoked on that. If you do something different with how you store your singleton beans in the application scope, you'll have to edit the code (it's near the bottom of ExtDirect.cfc, with the cfinvoke tags).
    2. Because during development, you're probably going to want to "refresh" the API at certain times, you're going to need to re-run the onApplicationStart() method in your Application.cfc file. I recommend adding this code to onRequestStart() so you can simply add a URL variable into the location bar whenever you want to refresh the API. Ex:
      <cffunction name="onRequestStart">
         <cfargument name="targetPage" type="string" required="yes">
         <!--- Reload the application if the reloadApp URL parameter is present --->
         <cfif structKeyExists( url, 'reloadApp' )>
           <cflock scope="application" type="exclusive" timeout="30">
             <cfset onApplicationStart()>
      Then you can run the url: to refresh the API.
    3. Depending on the number of directories in your site, and the number of CFC's that you have, the initialization routine may take a few seconds. This performance penalty is only for the application initialization request however, and all subsequent requests will not experience this delay.

    Any questions / comments / suggestions, leave a reply, and happy coding!


    P.S. Download the attached file in a non-IE browser. IE seems to corrupt downloads from the forum.
    Attached Files Attached Files
    Last edited by Gjslick; 31 Dec 2009 at 12:15 PM. Reason: Updated the file. Wasn't originally returning the correct json data for method calls which returned void.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts