So it's been a while since I blogged as time is something that seems to be eluding me. But I have some time today, and want to share something that I have been researching and playing around with.
Disclaimer: While I have some knowledge with these tools that I speak about, I certainly do not claim to be an expert with any of them. I learn something new every day and want to share my discoveries as I go, whether if helps someone or not. If it does not help anyone, I at least have an archive for my own records.
So… during my first experiences with the prototype AJAX framework, I found it rather simple to use and very easy to implement. But… as time went on, I found more and more reasons to try and expand on the framework with ColdFusion. The main thing that always seemed to pop up was to use it with ColdFusion Components (CFC) more easily. If any of you have ever used these two things together you must know that you cannot call a CFC directly with prototype, so a controller/file of some kind is needed to communicate between the two. Usually, this is in the form of a regular .cfm file that contains the code to create the components object and execute it somehow. I found it rather tedious to do this on a regular basis, and the number of files was starting to get a little out of hand since I am working on a fairly large application.
So I pondered the question, “how can I make prototype call a CFC?” The thought that came to mind was to create some sort of bridge, a gateway if you will, that generates your AJAX scripts dynamically on page load so that you can define what is being passed and how. So I decided to go with a custom tag for my first shot at this. My goal was to create something that anyone could learn quickly and start using immediately. So I decided to create a custom tag and have it generate your javascript on the fly, for simplicity sake and to also create a clickable link.
NOTE: My examples will be using <cfmodule> for simplicity; you will need to install the tag if you plan on using it as its own tag.
The tag has a few attributes, and it can be used in a couple of different ways, as seen in the following examples.
One way is to make a simple server request through a CFC. I have a container2update attribute in this tag to show some output for testing purposes. It is not required, and if it is left out you can simply call a method to update, insert or delete a database record.
<cfmodule
template="prototypeCFCGateway.cfm"
linkText="Say Hello!"
componentName="hello"
methodName="sayHello"
methodArguments="#FORM#"
useCFCinit="true"
AJAXOptions="evalScripts:true"
container2update="gateway">
Another way is to make a call to another file, which could process an entirely different thing (this is just a regular ajax call to a CFM file that returns some HTML that’s updated in a div container):
<cfmodule
template="prototypeCFCGateway.cfm"
linkText="Say Goodbye!"
url="ajaxCall.cfm"
AJAXParameters="test:1,testing:'it worked'"
AJAXOptions="evalScripts:true"
container2update="gateway2">
These two custom tag calls get the job done in almost 90% of the areas that I want to use AJAX. It has also reduced the amount of files I need by about 50%, which makes coding a little easier as there is less things to weed through.
This works similar to ajaxCFC which is much more in depth. This thing I wrote here is very, very simple and as you can see, the version of this “gateway” is only revision 0.1. This is the very first iteration and my plans are to create a CFC that can be cached into the application model and used when needed throughout the application. But for now, this gets the job done. Let me know if you have questions about it, and check back soon for updates.
NOTE: If you download, you are downloading at your own risk and can not hold me liable for any problems you may have using this.