Archive for the 'AJAX' Category

Microsoft picks JQuery

So I read today that Microsoft is going to adopt JQuery and include it in Visual Studio according to an eWeek article.  This sounds like a big coup for JQuery and I wonder what the folks at Adobe will do with Spry.  My perception has been that Spry hasn’t been hyped very much since it was released.  I never see any announcements about it and versions seem to come out rather slowly.  

I had played with what I thought were the big three AJAX libraries, JQuery, YUI and Spry.  I found Spry to be faster but not as polished as YUI and JQuery I actually found the hardest to use.  I personally believe that with Microsoft (and others) behind JQuery, it will become the dominant AJAX library and the one developers will want to know.  Then again, here I am working with Coldfusion most of the time and it is hardly the most wide spread web scripting language… so maybe it doesn’t even matter.  ;-)

Display XML Data with YUI DataTable Control

I’ve been evaluating various AJAX libraries lately, trying to determine which we’ll use for an upcomming project, one that will be our first foray into an AJAX based application.  I’ve looked at Adobe SPRY but most recently took a look at YUI (Yahoo User Interface).  Specifically I’ve been looking at how easily DataGrid or DataTable controls can be used. 

The YUI DataTable control is currently in a BETA release at the time of this writing.  Even so, it seems pretty complete and solid from my perspective.  In this example, I’m going to read the same dynamically generated XML I have used in my previous examples.  It could be static XML, but I’m generating data from a MySQL database using a Coldfusion CFC.  Lets get started.

One thing I like about YUI is that Yahoo will host all the libraries, requiring that you only include the libraries you need.  In this example, we’ll include the following style sheets and YUI libraries:

<link rel=”stylesheet” type=”text/css” href=”http://yui.yahooapis.com/2.5.2/build/fonts/fonts-min.css” />
<link rel=”stylesheet” type=”text/css” href=”http://yui.yahooapis.com/2.5.2/build/datatable/assets/skins/sam/datatable.css” />
<script type=”text/javascript” src=”http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js”></script>
<script type=”text/javascript” src=”http://yui.yahooapis.com/2.5.2/build/connection/connection-min.js”></script>
<script type=”text/javascript” src=”http://yui.yahooapis.com/2.5.2/build/element/element-beta-min.js”></script>
<script type=”text/javascript” src=”http://yui.yahooapis.com/2.5.2/build/datasource/datasource-beta-min.js”></script>
<script type=”text/javascript” src=”http://yui.yahooapis.com/2.5.2/build/datatable/datatable-beta-min.js”></script>

 I won’t break down what each library is responsible for, these are the libraries and style sheets that were used in the Yahoo example.  Just include them in the <HEAD> section of your HTML.

We need to tell it where to display the actual output.  In this case, we will use a div with id localxml:

<div id=”localxml”></div>

Next, we have the javascript that calls upon and uses the various functions from the above mentioned libraries:

<script type=”text/javascript”>
YAHOO.util.Event.addListener(window, “load”, function() {
    YAHOO.example.Local_XML = new function() {
        this.connectionCallback = {
            success: function(o) {
                var xmlDoc = o.responseXML;
                var myColumnDefs = [
                    {key:"id", sortable:true, formatter:this.formatUrl},
                    {key:"firstName", sortable:true, formatter:this.formatUrl},
                    {key:"lastName", sortable:true, formatter:this.formatUrl},
                    {key:"officePhone"}
                ];
                this.myDataSource = new YAHOO.util.DataSource(xmlDoc);
                this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_XML;
                this.myDataSource.responseSchema = {
                    resultNode: “row”,
                    fields: ["id","firstName","lastName","officePhone"]
                };
                this.myDataTable = new YAHOO.widget.DataTable(“localxml”, myColumnDefs, this.myDataSource);
            },
            failure: function(o) {
           }
        };
        this.getXML = YAHOO.util.Connect.asyncRequest(“GET”,
                “http://brad.melendy.com/projects/webservice/employees.cfc?method=FindAll“,
                this.connectionCallback);
    };
});
</script>

Now, there are a couple KEY spots here you must update for each XML document you plan to read into the DataTable:

  1. You must define the structure myColumnDefs to match your own XML document.  In this case as you can see above, our key headers are id, firstName, lastName and officePhone.  Also make note of the sortable:true argument.  These items will toggle asc/desc sorts on click.
  2. You must update the resultNode in the responseSchema to reflect the proper value from your XML document.  In this example, the name of our node is row
  3. You also must update the fields element of the responseSchema to match the values you outlined in myColumnDefs.  These are the same as item one above. 
  4. The last step is to update the asyncReqeust() function value, second argument to the location of our XML document.  This can be a static XML document residing on the local web server (relative path) or it can be a fully qualified URL as in our example above. 

That’s it really.  Once you have this in place and fire it up, you should see a very nicely formatted data table/grid:

http://brad.melendy.com/projects/ajax/yui/yui_data3.cfm

I wasn’t able to embed the sample in this post without the WordPress CSS breaking the YUI CSS so you will have to click the link above. 

I found Adobe SPRY to be a little faster to develop using Dreamweaver, however, YUI is nicer on the eyes with a lot of readily available style sheets.

Adobe Spry Dataset with Coldfusion Components

I recently started playing with Adobe Spry while evaluating some AJAX options for work.  In an attempt to cover the basics of a CRUD application that uses Spry, I made the first step of displaying records from a database.

Spry is great at reading data from XML and since we can’t read data direct from the database, we need a coldfusion component.  The CFC will read the database, loop from records and build an XML output.  Here is my very basic CFC:

<cfcomponent>
<cffunction name=”FindAll” access=”remote” returntype=”xml” output=”no”>
<cfquery name=”getEmp” datasource=”#dataSrc#”>
SELECT firstName, lastName, officePhone, id
FROM employees
</cfquery>
<!— Convert Query to xml —>
<cfprocessingdirective suppresswhitespace=”Yes”>
<cfcontent type=”text/xml; charset=utf-16″>
<cfxml variable=”xmlobject”>
<response>
<data>
<cfoutput query=”getEmp”>
<row>
<id>#id#</id>
<firstName>#firstName#</firstName>
<lastName>#lastName#</lastName>
<officePhone>#officePhone#</officePhone>
</row>
</cfoutput>
</data>
</response>
</cfxml>
<!— Convert back to a string —>
<cfset myvar=toString(xmlobject)>
<cfset mynewvar=replace(myvar, “UTF-8″, “utf-16″)>
</cfprocessingdirective>
<cfreturn mynewvar>
</cffunction>
</cfcomponent>

I ran into some compatibility issues with the XML document created by the CFC.  Initially I kept it the same as my flexbuilder example, however, the XML produced wasn’t good enough for Spry?  Interestingly, I needed to be a little more strict in my XML creation for Spry to accept it as a valid document.  Funny that two Adobe products would have different criteria for what is an acceptable XML document.  The only way I could make it work with Spry short of actually pointing to a static XML file was to use the <cfxml> tag to create my document.  Flexbuilder did not require this and seems to be more loose in it’s requirements.

The CFC can be called directly in your browser and display the XML output:

http://brad.melendy.com/projects/webservice/employees.cfc?method=FindAll

Now we need only create our page with the Spry libraries like so:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xmlns:spry=”http://ns.adobe.com/spry”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
<script src=”../../../SpryAssets/xpath.js” type=”text/javascript”></script>
<script src=”../../../SpryAssets/SpryData.js” type=”text/javascript”></script>
<script type=”text/javascript”>
<!–
var ds1 = new Spry.Data.XMLDataSet(“http://brad.melendy.com/projects/webservice/employees.cfc?method=FindAll”, “response/data/row”,{sortOnLoad:”id”,sortOrderOnLoad:”ascending”,distinctOnLoad:true});
//–>
</script>
</head>

<body>
<div spry:region=”ds1″>
<table>
<tr>
<th spry:sort=”id”>Id</th>
<th>FirstName</th>
<th>LastName</th>
<th>OfficePhone</th>
</tr>
<tr spry:repeat=”ds1″ spry:setrow=”ds1″>
<td>{id}</td>
<td>{firstName}</td>
<td>{lastName}</td>
<td>{officePhone}</td>
</tr>
</table>
</div>
</body>
</html>

You will notice that on the line where we create our new dataset object, I use the full URL to the CFC.  This could be a relative link to the file on the same web server, but it needn’t be, in fact it can reside on an entirely different server.  In this example, our three important areas include the two Spry javascript libraries, xpath.js and SpryData.js, the creation of the dataset, “ds1″ and then finally the output of the spry:region.  I have set the ID column to be sortable with “spry:sort”.  The headers are static, and then “spry:repeat” is used to display all the records just like <cfoutput query=”"> would do.  Here is the working demo:

http://brad.melendy.com/projects/ajax/spry/test2.cfm

It isn’t pretty as there is no style but it does the job and only takes about 15 minutes from start to finish.  I’ve included all the files for the working demo here:

http://brad.melendy.com/projects/ajax/spry/spry-dataset-cfc.zip

Just extract onto your web server and make sure that you create the DB using the included .SQL file and you will need to setup your datasource in CF.