ColdFusioning.com
Pronunciation \kold-fy-zhn-ing\ Noun: The actions of one that writes ColdFusion.

Please check out my new site: http://ja.mesbrown.com/

jQuery Templates with ColdFusion ORM and AJAX

In this post, we'll look at how to use the new jQuery templates in conjunction with ColdFusion and AJAX. If you haven't already, check out this previous post on using jQuery templates without AJAX.

If you want to skip ahead, all of the files are on github (under clienttemplates) and the working examples can be found on this link.

To keep things simple, we're going to use the cfartgallery datasource that comes with ColdFusion and we're going to use simple ORM. If you don't have the sample database installed, this example won't work, but you'll be able to get the idea.

The first thing we're going to do is setup our Application.cfc so we can use ORM:

Application.cfc:

view plain print about
1component{
2    // Application properties
3    this.name = hash(getCurrentTemplatePath());
4    this.sessionManagement = true;
5    this.sessionTimeout = createTimeSpan(0,0,30,0);
6    this.setClientCookies = true;
7    this.datasource = "cfartgallery";
8    this.clientManagement = true;
9    
10    this.ormenabled = "true";
11    this.ormsettings = {
12             logSQL             = false
13             ,dbcreate            = 'update'
14    };
15}

This is pretty straightforward Application.cfc and ORM. The important parts are setting the datasource and setting ormenabled to true. If you aren't familiar with ORM, you should view Introduction to ORM or ColdFusion ORM Presentation.

The next step is to map our ORM object to the existing table:

artists.cfc:

view plain print about
1component    persistent        = true
2            accessors        = true
3            entityname        = 'artists'
4            {
5
6    property name='id'                 column='ARTISTID'         generator='increment';
7    property name='FIRSTNAME';
8 property name='LASTNAME';
9 property name='ADDRESS';
10 property name='CITY';
11 property name='STATE';
12 property name='POSTALCODE';
13 property name='EMAIL';
14 property name='PHONE';
15 property name='FAX';
16 property name='thepassword';
17}

This is the simplest implementation of this object since the datasource and table already exists.

In the next step, we're going to setup our remote cfc call. This is where the power of ColdFusion ORM comes into play:

remote.cfc:

view plain print about
1component {
2    remote string function getArtist(){
3        return SerializeJSON(entityLoad('artists'));
4    }
5}

That's all that is required to load the entire artists table and serialize it to JSON, so we can return it in the AJAX call. Very simple.

Lastly, we're going to repeat a lot of code from the previous jQuery templates post. I won't repost everything here but we'll look at the two key features we're using in this example.

The first key feature is our actual ajax call. We're going to call our remote.cfc to get our JSON. Then we're going to invoke the jQuery template and append it to our empty div in ajax.cfm:

view plain print about
1<script type='text/javascript'>
2$(function(){
3    $.getJSON('remote.cfc?method=getArtist'
4        ,{returnFormat: 'json'}
5        ,function(data){
6            $('#artistTemplate')
7                .tmpl(data)
8                .appendTo('#theContacts');
9    });
10});
11</script>

Lastly, we have our actual jQuery template. We get a little more advanced in this example and introduce an "if" conditional. This causes the phone and fax to only display if they exist in the data returned from our AJAX call. This is extremely powerful and allows us to use logic in our templates without complex JavaScript:

view plain print about
1<script id="artistTemplate" type="text/x-jquery-tmpl">
2    <div>
3        Name: ${ FIRSTNAME } ${ LASTNAME } <br />
4        EMail: ${ EMAIL } <br />
5        Address: <br />
6        ${ ADDRESS } <br />
7        ${ CITY }, ${ STATE } ${ POSTALCODE } <br />
8        {{if PHONE}}
9            Phone: ${ PHONE } <br />
10        {{/if}}
11        {{if FAX}}
12            Fax: ${ FAX }
13        {{/if}}
14    </div>
15</script>

If you view the source code of the example or explore the code on github, you'll see there are a few other sections for formatting, including jQuery and the template JavaScript.

Try running the code and let me know what you think.

TweetBacks

Comments

When you have conditional logic in your jQuery template I find string concatenation can be a lot easier. One nice thing with templates is that the data object is automatically available in $.tmplItem().

If you use $.getJSON then the dataType is expected to be JSON - there is no "returnFormat" parameter for $.getJSON() as far as I know.

I'm not sure of syntax for scripted CFCs but you should be able to add returnformat="json" or in the request url add returnformat=json and not need to use serializeJSON(). That way you CFC can be used by other app that do nto use JSON.

# Posted By johans | 11/25/10 3:29 PM

Recent Comments

RSS

Subscribe