Rob Gonda's Blog

ajaxCFC for Model-Glue Explained

While this is not the final documentation, I know some people are starting to play with ajaxCFC/MG and my implementation is not straightforward. This is my first draft to get the feeling on how to document it.

Ajax.cfc is just another model in your MG application, used by the controller to manipulate data. You can initialize it in your Controller’s init function and store in persistently in the controller’s variables scope.

The Ajax remote request call flows exactly (or almost) like a traditional call. It flows as follow:

  1. Page triggers an AJAX request.
  2. The event will be caught by the MG framework.
  3. The MG event-handler will broadcast a message.
  4. The controller will pickup the message.
  5. The controller will AJAX model to parse it (onRequestStart)
  6. The controller can use other models at will (theEvent)
  7. The controller uses the AJAX model to prepare the reponse.
  8. The controller sets the ‘ajaxResponse’ value back to be used by the view
  9. The modelGlue.xml includes ajaxResponse.cfm as the view that will transmit the response back to the JavaScript callback function.

Alright, so this may sound complicated, but it really isn’t. The flow is exactly like any other page with the exception of:

  1. You need to parse the Ajax request with the Ajax model.
    1. You will received a structure with two elements

                                                              i.      id: the unique identifier of the Ajax remote call request

                                                            ii.      params[type array]: this includes all arguments passed by the JavaScript function. Because the arguments are unnamed, you will receive them in an array in the same order that they were passed.

    1. My example parses the values in the controller’s onRequestStart and place them in the request scope to be picked up later by the requested method.
    2. Optionally, you may invoke security and debugging functions contained in the Ajax model.
  1. You need to prepare the response before sending it back to the framework. You will use the Ajax.returnAjax method to do so. The returnAjax method always takes your response and the Ajax call unique identifier.
  2. You need to set the ajaxResponse value to be used by the view.
  3. Finally, include the ajaxResponse.cfm file that simply displays the ajaxResponse value sent by the controller.

Your JavaScript callback function will receive any response you sent as an argument.

That is pretty much the basis; nothing fancy, nothing different than what you usually do with the MG framework. Ajax.cfc is just facilitating the seamless communication between your JavaScript and ColdFusion side; that is all.

Note that the main event (the one making the Ajax call) needs to include the ajax.js file, the _ajaxConfig JavaScript object, and of course, your JavaScript calls and callback functions.

While I do not recommend leaving business logic at the client side, because it defeats the entire MVC and thus MG framework, it is the easiest way to get started with Ajax.

In my next example, I will show how to leave all the business logic in the server side, contained in views, and leave only connectivity functions in the client side.

If you have any comments / requests, do not hesitate to comment or contact me and I will do my best to add them to the framework.

There are no trackbacks for this entry.

Trackback URL for this entry:

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
This blog is running version 5.9.003. Contact Blog Owner