Categories
Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...
Web Hosting

<< May, 2013 >>
SMTWTFS
1234
567891011
12131415161718
19202122232425
262728293031
Search Blog

Recent Comments
Re: Disappearing IE Popup Window During Save/Open Dialog (by LZ at 4/20 7:58 AM)
Re: Create Dynamic WHERE Clauses in PHP (by pooja at 3/20 7:29 AM)
Re: Just What IS a 'Service Layer', Anyway? (by EugenK at 3/07 7:56 PM)
Re: Using Google as your CF Mail Server (by 5starwebteam.com at 2/25 1:27 AM)
Re: Why Provide for Service layer objects in CFWheels? (by Steven Benjamin at 1/25 11:43 AM)
Re: What is an 'Advanced' Coldfusion Developer? (by ColdFusion Developer at 12/24 5:14 AM)
Re: Equivalent of SQL "TOP X" in Oracle (by Ashenafi Desalegn at 12/06 5:29 AM)
Re: PHP Export to Excel Snippet (by serene at 12/05 1:44 AM)
Re: Just What Is 'Application Logic', Anyway? (by Arif at 11/13 8:06 AM)
Re: Hosts File Changes Not Acknowledged on Vista 64 (by Aaron at 10/22 2:31 PM)
Re: PHP Export to Excel Snippet (by Jafar Shah at 10/10 4:28 AM)
Re: Viewing Option Text (in IE7) that's Wider than the Select List (by Chenelle S at 10/04 12:53 PM)
Re: PHP Export to Excel Snippet (by Kilo at 9/26 5:20 PM)
Re: Porting Coldfusion Code to Mura (by tariq at 9/03 9:51 AM)
Re: Just What IS a 'Service Layer', Anyway? (by James at 8/27 4:06 PM)
Re: Calculating Business Hours (by helen at 8/14 2:54 AM)
Re: What IS 'Business Logic', Anyway? (by dougboude at 8/06 11:30 AM)
Re: What IS 'Business Logic', Anyway? (by Adrianne at 8/06 10:29 AM)
Re: Family Law: The Weapon of Choice for Woman Scorned (by dougboude at 8/04 4:39 PM)
Re: Family Law: The Weapon of Choice for Woman Scorned (by Lola LB at 8/04 7:43 AM)
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

Powered by
BlogCFM v1.11

14 January 2011
Using Ajax in Mura

Part of the process of porting our company web site over to Mura involved including some ajax functionality. I spent a few hours researching and experimenting, so thought I'd share what I learned in case it saves someone else some time later.

The Scenario
The site you're porting to Mura currently retrieves and outputs a list of scheduled events, and you need to have that same ajax call working within the Mura site as well.

outputting events via ajax call in mura

How it Works In a Nutshell
In order to accomplish our goal, we need to:

  • get our JS library loaded;
  • get our custom ajax JS code loaded;
  • get our backend code (that responds to the ajax calls) in place;
  • keep our ajax code modular so that we have flexibility with what pages we include it on and in what position.

Loading our Library and custom JS
The fact is, Mura already by default has JQuery loaded up and ready to go (if you'd like to change your default library to something else, such as Prototype, check out step 1 of this Mura article). As you know, however, the order that js files are loaded up is very important. For instance, loading up our custom JS file before our library gets loaded means that our custom js code will not execute and will likely error out. So, you have a couple of options here:

1. You can put your JS code directly within the page or component so that it will always be executed after JQuery is loaded (if this suits your needs, you can simplify your life and skip to the Simpler Method);

2. You can put your js code into an external file, then control the order that files are loaded up in the page using special Mura commands.

Keeping our code modular
The obvious answer to this one is to utilize a Mura Component to contain our Ajax. We'll call this component "Show Events". This component will contain the div that our ajax call will populate with content as well as the js code that does the populating.

Backend Code
My backend code for this exercise is a simple CFM page named registrationAjax.cfm that contains a cfswitch/cfcase statement that responds to ajax calls. I will be placing it in the following path:
[mura installation folder]\[site name]\includes\display_objects\custom\registrationAjax.cfm

 

THE DETAILS
Okay, let me show you how I put this all together, elaborating along the way as I feel the need.

Backend Code
As mentioned previously, I dropped my backend code into the [mura installation folder]\[site name]\includes\display_objects\custom folder. Your backend code may very well live in a galaxy far, far away; not a prob. It's ajax, man! If your backend code lives elsewhere, just forget about this step.

ajax backend code for mura

The Ajax Component
Okay, our component is going to include the target div where our retrieved event data will be output:

<div class="fancyheader" style="text-align: left; ">UPCOMING EVENTS</div>
<div id="eventTarg"></div>

We'll be adding a little something more to this component in the next step.

Loading the JS File
Just FYI, here is the content of my js file:


$(document).ready(function() {
 var params = new Object;
 params.meth = "getEventLinks";
 $('#eventTarg').load('includes/display_objects/custom/registrationAjax.cfm',params);
});

 

 When the page this code lives in finishes loading, an ajax call will be made to my backend code, the result of the call being placed into the 'eventTarg' div.

Next we need to place our js file somewhere. I opted to create a "custom" folder under my site's "js" folder, like so:
[mura installation folder]\[site name]\js\custom

location of custom js mura file

Bogus (yet necessary) Step

Here we come to a step that I feel is kinda bogus to have to do, but never the less it's required by Mura when you want your JS to live in an external file. Rather than us just being able to add a string like "<script src="[path stuff]/myjsfile.js"></script>" to the head of our page, Mura requires us to create a CFM template that itself contains that string, then we add that cfm page to the head. I know, sounds (and is) a bit overcomplicated, but this is how you have to do it if you want your JS in an external file. SO, I added a "custom" folder to my [mura install folder]\[site name]\includes\display_objects\htmlhead\ folder. In there I created "loadShowEventJS.cfm" which contains the following code:

<cfoutput>
 <script src="#$.siteConfig('assetPath')#/js/custom/showEvents.js" type="text/javascript"></script>
</cfoutput>

mura dump ajax step

 

 

 

 

Now that we have that file created and in place, we need to revisit our component and add two lines that will put our external js file into the page's head AND ensure that it is loaded AFTER the JQuery library. Add these two lines to your component:


[mura]$.loadJSLib()[/mura]
[mura]$.addToHTMLHeadQueue('htmlHead/custom/loadShowEventJS.cfm')[/mura]

 

 mura ajax component

This is an excellent time to point out the use of Mura's addToHTMLHeadQueue function. It takes a path (relative to the display_objects folder in your site). Whatever the final rendered content of the targeted CFM file is, that is the string that will be included in the head of your page. Notice we're also using the loadJSLib method to ensure that our js library (JQuery in this case) is loaded before our custom js file. Even though by default Mura is ALREADY loading JQuery, if we don't use the loadJSLib() method here, JQuery would load AFTER our custom js file. For more information on the addToHTMLHeadQueue method (and every other Mura method), I highly recommend you bookmark the developer mura docs

That's It!

I know, it wasn't quite painless. But at this point you can go ahead and test it all by adding your component to a page and then viewing it. There are a lot of moving parts to this, like paths and your ajax code itself, so just bear that in mind if things don't work quite right the first time 'round.

 
Simpler Method
If having your JS live in an external file isn't a big deal to you, then there IS an easier way to get your ajax functionality into your page. Remember I told you that the JQuery library was already being included by default? Well, if we opted to put our JS code inline within our page body, then it would work just fine without us having to tell Mura where to load JQuery within the head. So in this scenario, we need only modify our component code to look like this:

<div class="fancyheader" style="text-align: left; ">UPCOMING EVENTS</div>
<div id="eventTarg"></div>
<script>
$(document).ready(function() {
 var params = new Object;
 params.meth = "getEventLinks";
 $('#eventTarg').load('includes/display_objects/custom/registrationAjax.cfm',params);
});
</script>

Including that component on any page will now accomplish the same thing it took several additional steps to do the "proper" Mura way.

Hope this helps someone!

Doug out.




Posted by dougboude at 3:27 PM | PRINT THIS POST! |Link | 1 comment
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: Using Ajax in Mura
Thanks for pointing out the simpler method. The main reason to use the addToHTMLHeadQueue() method is when you have some js or css code that is shared between multiple widgets that that may or may not be on the same page. In that scenario it eliminates the duplicate code. This is because Mura will eliminate duplicate entries int he queue.

Also in 5.4 we added addTOHTMLFootQueue().
Posted by Matt Levine on January 14, 2011 at 6:27 PM

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

Seventeen plus Twelve equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!