Posted by & filed under programming.

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.

prototypeCFCGateway.rev0.1.zip (3.00 kb)

Posted by & filed under software.

Since I am a big fan of Google's products and services, I've started using Google Chrome. For those of you who don't know, Chrome is an Internet Browser just like Mozilla Firefox, Microsoft Internet Explorer and Apple's Safari. This is my unofficial review of the browser after using it for a couple of weeks.

 

  1. The engine behind it.  Like Safari, Chrome uses the Webkit engine, not that I know anything about this stuff, but I thought I would share.  Firefox uses the Gecko layout engine, and IE uses their own of course.  But what I have read, is that Webkit is fast and lightweight, allowing users to get online faster, and have a more pleasant browsing experience.
  2. I've been using Chrome for all my Google products, i.e. Gmail, Analytics, Adsense, AdWords, etc…  Chrome is at least 50% faster for these things, as I'd expect being part of Google.  Pages snap into view quickly, while in IE, it is sometimes frustrating at how long it can take to view a simple page.  Google claims the time savings is because they "built a more powerful JavaScript engine, V8, to power the next generation of web applications that aren't even possible in today's browsers".  It seems to be working, at least for what I am doing.
  3. The layout is plain, simple, and streamlined, much like googles other products, so you can do what you signed on to do faster, surf.  I like the Omnibar too, which is the address bar acting as an internet search field as well.
  4. Extremely light on the system load.  I had a Chrome browser open with 3 tabs, and I opened the same tabs in IE and Firefox.  The results were like this. Chrome addressed 33mb of memory.  IE7 was using 162mb!! of memory OMG!! and Firefox was using 46mb.  So you can see chrome is much lighter, and for the average PC having 512mb of memory, these savings are huge over IE.  Firefox does a good job too, but the Webkit engine is obviously smaller than the Gecko engine after seeing this test.

 

Being a beta version, I'd expect some problems.  One thing is a major security concern that has poppep up, read the article for an explanation.  But one thing that has bothered me is that sometimes Chrome hangs up.  It will sit there for a minute or so, and then go back to normal, not sure why or what is going on, but it does it randomly, not just on page clicks.  I am filing this one under a beta release bug, but the home button thing, I think they should have.  Oh, and the home button needs to be turned on in the options, I think this should be defaulted to on.  I found it while playing around, thankfully.  I was really disappointed that there was no home button. I've made my own home page with my links I use all the time, and it was a pain that I could not get to it easily.

In Conclusion, I give it a thumbs up for the beta version. I can't wait to see what the actual production release will do, we will have to wait and see. As for IE and Firefox.  I am hoping IE8 fixes the inherent problems found in IE7, memory addressing being a huge one.  Firefox is still extremely popular in the development world, because of it's massive library of extensions and plugins.  I use Firefox for application debugging.  I hope google opens up some developer api's, but again it's still to early to tell while it's still in Beta.

Posted by & filed under programming.

When I was a noob (new user) to coldfusion, I racked my brain, and spent countless hours trying to figure out when to use which.  So after speaking to many developers, and doing countless tests of my own, I will give you my take.  Hopefully you will find this information useful, and if you are a new user, maybe save you some time when writing your first application.

These will be rather simple examples, so if you have a more in-depth issue, please post your problem here and I will do my best to answer that for you.

First, we'll start off with CFLOOP, it's different types and it's most common attributes.

(Curtesy of Adobe Livedocs)

  • cfloop: index loop
  • cfloop: conditional loop
  • cfloop: looping over a date or time range
  • cfloop: looping over a query
  • cfloop: looping over a list, a file, or an array
  • cfloop: looping over a COM collection or structure

We are only going to cover index, lists, and query type loops here for time sake. And this is just a basic overview to give you an understanding of CFLOOP so that when I compare it to CFOUTPUT, you will see the difference a little easier.

First, we'll descuss an index list. 

It is the most basic loop, where you specify from: to: values and assign them to an index variable.  There is also an optional step attribute that allows you to specify what number to increment by.  i.e. if you have a loop from 0 to 120, and you set a step of 3, it would set the index value to 0,3,6,9… 120.

syntax
<cfloop index="parameter name" from="beginning value" to="ending value" [optional: step="increment"]>
HTML or CFML code …
</cfloop>

The best example for this would be if you knew how many times to loop or if you passed an integer to loop to for specific processing.  Maybe you want to loop 10 times to get 10 random numbers in a list.

<cfset randomList = ""> //empty list to append data to
<cfloop index="i" from="1" to="10">
<cfset randomList = listAppend(randomList,i)>
</cfloop>
<cfoutput>#randomList#</cfoutput> 

Next, a query loop is exactly what it says. 

It loops over the records in a query so you can display the data.

syntax
<cfloop query = "query name" [optional: startRow = "row number" endRow = "row number"]> //the startRow and endRow are used for things such as adding pagination to your output
HTML or CFML code …
</cfloop>

Example:

<cfquery name="qMyTable" datasource="#application.dsn#">
SELECT recordID,detail
FROM myTable
</cfquery>
<table>
<tr>
<td>Record</td>
<td>Detail</td>
<td>Delete</td>
</tr>
<cfoutput>
<cfloop query="qMyTable">
<tr>
<td>#qMyTable.recordID#</td>
<td>#qMyTable.detail#</td>
<td><input type="checkbox" name="Checkbox" value="#qMyTable.recordID#"></td>
</tr>
</cfloop>
<cfoutput>
</table> 

You still have to wrap your loop in CFOUTPUT tags, so that your variable will show.  I wrap the entire loop to use less code, but you could wrap each variable independantly if you wanted to, both will work.

Anyway… you should end up with an output that looks like this:

Record Detail Delete
1 Record 1 Checkbox (value=recordid)
2 Record 2 Checkbox (value=recordid)
3 Record 3 Checkbox (value=recordid

Finally, the list loop.

This loops over a list of any kind, and assigns each list element to the index variable (list example:  'coldfusion,8,is,awesome' )  //Does not have to all be the samedata type

syntax
<cfloop index="index name" list="list items" [optional: delimiters="item delimiter"]>
HTML or CFML code …
</cfloop>

The best example for this would be if you had many form fields, such as checkboxes, that you wanted to pass to an action page(old way) or model(new MVC way) to do a mass update or deleting of records.

If you take the table from the last loop I just discussed:

Record Detail Delete
1 Record 1 Checkbox (value=recordid)
2 Record 2 Checkbox (value=recordid)
3 Record 3 Checkbox (value=recordid)

Now if you happen to check checkbox 2 & 3 to delete.  You will have a field variable called checkbox with a list of record id's. So we will loop over this form field to delete the checked records.  Here is the code to do so.  Note: if a checkbox is not checked, the field is not passed in the FORM scope thus leaving it blank.

<cfloop index="i" list="#FORM.Checkbox#">
<cfquery datasource="#application.dsn#">
DELETE FROM myTable
WHERE recordID = #i#
</cfquery>
</cfloop> 

Now, these loops are the most common, but you can loop on just about anything in CF, i.e. collections, files, arrays, dates, etc… but that's for another day and another blog post.

Now to explain CFOUTPUT with a query attribute.

syntax
<cfoutput query="query name" [optional: group="query column" groupCaseSensitive="yes|no" maxRows="maximum rows to display" startRow="start row"]>
HTML or CFML code …
</cfoutput>

This is very comparable to the second loop above, the loop with a query. The only difference being what you intend to get for an output.  You could duplicate the loop above and make it look like this instead

<cfquery name="qMyTable" datasource="#application.dsn#">
SELECT recordID,detail
FROM myTable
</cfquery>
<table>
<tr>
<td>Record</td>
<td>Detail</td>
<td>Delete</td>
</tr>
<cfoutput query="qMyTable">
<tr>
<td>#qMyTable.recordID#</td>
<td>#qMyTable.detail#</td>
<td><input type="checkbox" name="Checkbox" value="#qMyTable.recordID#"></td>
</tr>
<cfoutput>
</table>

This works too, but I tend to use CFLOOP for these sort of simple outputs.  I use CFOUTPUT when I need to group fields and still show their associated data.  I'll explain.

If we expand on the above query to make it include multiple phone numbers for each record like so:

<cfquery name="qMyTable" datasource="#application.dsn#">
SELECT a.recordID,a.detail,b.phone
FROM myTable a INNER JOIN phoneNums b ON a.recordID = b.recordID
</cfquery>

With 3 records in myTable, and 2 phone numbers each, we will have a total of six records and the query output will look like this:

Record ID Detail Phone
1 Record 1 Phone1
1 Record 1 Phone2
2 Record 2 Phone1
2 Record 2 Phone2
3 Record 3 Phone1
3 Record 3 Phone2

Now… say we want to show three records with each phone number.  Looping over this query with CFLOOP would require nested CFLOOPS and sub-queries, which is not ideal. So we will use CFOUTPUT with a query attribute and a group attribute.  Here is the code:

<table>
<tr>
<td>Record</td>
<td>Detail</td>
<td>Delete</td>
</tr>
<cfoutput query="qMyTable" group="recordID">
<tr>
<td>#qMyTable.recordID#</td>
<td>#qMyTable.detail#</td>
<td><cfoutput>#qMyTable.phone#<br></cfoutput></td>
</tr>
<cfoutput>
</table>

The nested cfoutput will loop over the records for the current recordID to display the multiple phone numbers.  The table will look like this:

Record ID Detail Phone
1 Record 1 Phone1
Phone2
2 Record 2 Phone1
Phone2
3 Record 3 Phone1
Phone2

This is a time saver over the nesting of CFLOOP's. You can even nest your CFOUTPUT's further than this, grouping a second time and outputting multiple records for that specific group. This is advanced, but if you would like to see an example, let me know.

I hope this helps any noobs or anyone who is a seasoned user that never understood the difference.

Posted by & filed under programming.

My problems with CFFLUSH and ColdFusion 8 are baffling me. I am trying to display an animated .gif file while my page is loading, but for some reason it does not show. I have installed all the updates and hotfixes for coldfusion, but to no avail, it still does not work. So.. I am forced to use AJAX. Hopefully ColdFusion update 2 or another hotfix will remedy this issue, who knows, I may be missing something else all together. :)

I did find another blog posting over at orangepips that discusses a strikingly similar issue, not sure if this is completely related to my issue, but it is some good information;

Unfortunately, the solution on this blog doesn't work for me either, so I will give a quick example of what I did with AJAX to get around this.

2008/8/14: EDIT::: I just did some more research and found out what the real
problem is. Since I have moved to ColdFusion 8, I also moved to
Windows Server 2003, which has IIS 6, which I have http GZIP compression
enabled. Through some web searching I found that since IIS will
compress the whole data output and then send it to the client, it does
not provide enough data to CFFLUSH to say "Hey, flush this data until
the page loads". It simply shows a blank screen until the page
suddenly loads. If anyone knows away to get this to work with CFFLUSH, please
comment. But for now, my process below definitely works and just may be the way of the future.

I will be using my AJAX framework of choice, Prototype. Prototype is one of the most popular frameworks out there and is very easy to use and has a smaller learning curve compared to some of it's competitors. I highly recommend it. As a side note, I also use Scriptaculous, which works very well with prototype. Scriptaculous is for creating site animations, auto-complete searches, and drag and drop features.

Okay.. Lets get started. This is a very crude before and after, but it should get my point across.

Before.. (1 File) (eventually this will be merged into an MVC framework, but this is legacy code I am dealing with, so it's just a single file for now)

"Coldfusion code (queries, object creation, and any other things that take long)"
<!--- Show the loading image --->
<div ID="loaderIMG" align="center">
<strong>searching</strong> <img src="images/loader.gif" border="0" />
</div>
<CFFLUSH>
<!--- To hide the image --->
<SCRIPT>
document.all.loaderIMG.style.display='none';
</SCRIPT>
"Display the HTML results down here"

After.. (2 files)

Search.cfm

<html>
<head>
<SCRIPT src="/js/prototype/prototype.js"></SCRIPT>
<script>
function doSearch(){
var criteria = $F('criteria'); //Prototypes way of getting form field values
var category = $F('category');
$('results_container').innerHTML='<div align="center"><strong>searching</strong> <img src="images/loader.gif" border="0" /></div>';
var options = {parameters : {criteria:criteria, category:category}, method:'get', onComplete:showSearch, onFailure:reportError};
new Ajax.Updater($('results_container'),'SearchResults.cfm', options);
return false;
}
// For returning an error to the user if the ajax call fails
function reportError(request){
$('results_container').innerHTML='<b><font size="4" color="red">! Sorry. There was an error.<b></font>'
}
</script>
</head>
<body>
<div id="master_container">
<div id="search_form" style="width:600px;">
<fieldset>
<legend>Quick Search&nbsp;</legend>
<form name="search" method="get" style="display:inline;">
<input type="text" name="criteria" maxlength="50" size="20">
<select name="category">
<option value="U">Users</option>
<option value="C">Customers</option>
<option value="S">Suppliers</option>
</select>
<input type="button" name="search" value="search" onclick="doSearch();">
</form>
</fieldset>
</div>
<div id="results_master_container" style="width:600px;">
<fieldset style="height:473px;">
<legend>Search Results&nbsp;</legend><br />
<div id="results_container">Perform your search...</div>
</fieldset>
</div>
</div>
</body>
</html>

SearchResults.cfm

"Coldfusion code (queries, object creation, and any other things that take long)"
"Display the HTML results down here"

What happens here in the after section, is when you click "search" on AjaxCall.cfm, it calls the doSearch(); function. This function includes some things to process the request. First it includes the values from the form to use for the search by settings vars of "criteria" and "category". Next.. the code line of "$('results_container').innerHTML='<div align="center"><strong>searching</strong> <img src="images/loader.gif" border="0" /></div>';" updates my search results container with the loading image. This will display until the AJAX call is complete. The next line sets some parameters and options for the call. It's not necessary to make it it's own variable, you could put it right into the AJAX call, but I find that it looks cleaner this way and is easier to debug.

Now for where the magic happens. The line "new Ajax.Updater($('results_container'),'SearchResults.cfm', options);" calls on SearchResults.cfm to do the search and create the HTML output. Once processing is complete, it updates the results_container div with the HTML that was generated. I also threw in an example of how to return an error to the user if the ajax call fails. As you can see, it's not that difficult. This will also help with the merge to an MVC framework, now that this is basically the model and the controller. The view will just have to be separated out. But that is for another posting in the future.

If you want to make your own loading images, you can go to AjaxLoad.info. You can make many different types there and it's much easier than making your own.

Posted by & filed under programming.

For the longest time, I've pondered the question, which one is best to use when?  I will break these down a little and give you my preference. 

For you people who have been programming for a long time, and have used many different types of programming languages, you may already have a preference, so please chime in with any opinions.  These tags are basically just a tag form of common 'if' and 'switch' functions used in other languages such as Javascript, C#, etc…  There is also a function form of these that can be used within the <CFSCRIPT> tag, but for simplicity sake, I won't be discussing that.  Although if you are interested in this I can give you my thoughts.

<CFIF>
<CFIF> is your basic tag for if and else statements.  Now… these sort of statements are used everywhere, and they are used in a lot of different technical applications.  Mostly ones that involve math.  These can be used in common applications such as excel, but also are available in highly technical applications like Unigraphics CAD/CAM (3D modeling engineering and manufacturing application) Something I used in my old profession.

Lets begin by breaking down the code a little to get a better understanding of what these tags do.  Take the following statement:

<CFIF request.qLabels.Title NEQ "Coldfusion Freak">....content....</CFIF>

This is a string validation/conditional statement, there are other types which I will get into in a minute, but this is checking a 'title' value from a query called 'qLabels' in the request scope against a basic string text of 'Coldfusion Freak'.  The 'NEQ' is basically an acronym for NOT EQUAL, so whatever is indside this tag will only get displayed if 'request.qLabels.Title' is not equal to 'Coldfusion Freak'. Simple, basic, and effective to get the desired output. What about the other types I mentioned?

<CFIF request.qLabels.ID EQ 12345>....content....</CFIF>
<CFIF NOT request.qLabels.deleted>....content....</CFIF>

The first one here is a numeric check using the EQ(equal) operator, notice the lack of quotes denoting that it is infact a numeric value.  The second one here is a boolean check, to see if the boolean value is true.  If it's NOT true, then it will display the content. These are just a couple examples, and are the most common that I have come across. To learn more about <CFIF> visit the Adobe Livedocs

<CFSWITCH> & <CFCASE>
Now this is a more sophisticated tool for checking a variable for multiple outcomes.  I will break down the code again.

<CFSWITCH expression="#URL.pageToView#"> 
<CFCASE value="services"><cfinclude template="services.cfm"></CFCASE> 
<CFCASE value="products"><cfinclude template="products.cfm"></CFCASE> 
<CFCASE value="cart"><cfinclude template="cart.cfm"></CFCASE> 
<CFDEFAULTCASE><cfinclude template="home.cfm"></CFDEFAULTCASE>
</CFSWITCH> 

This is a basic navigation case where it checks the URL variable 'pageToView' to know what page to include for output.  As you can tell, this will be useful for any circumstance that has one variable the could have many different values.  A whole framework could be written around this and it would be very easy to debug and follow.

Now lets compare them with usage that will give you the same outcome (i'll use the same example I just used):

<CFSWITCH expression="#URL.pageToView#"> 
<CFCASE value="services"><cfinclude template="services.cfm"></CFCASE> 
<CFCASE value="products"><cfinclude template="products.cfm"></CFCASE> 
<CFCASE value="cart"><cfinclude template="cart.cfm"></CFCASE> 
<CFDEFAULTCASE><cfinclude template="home.cfm"></CFDEFAULTCASE>
</CFSWITCH>


<CFIF URL.pageToView EQ "services"> <cfinclude template="services.cfm"> <CFELSEIF URL.pageToView EQ "products"> <cfinclude template="products.cfm"> <CFELSEIF URL.pageToView EQ "cart"> <cfinclude template="cart.cfm"> <CFELSE> <cfinclude template="home.cfm"> </CFIF>

These will both have the same outcome, so why choose one over the other? It really boils down to a preference.  In this example, it is rather simple, so the performance is not really an issue, but while coding, the <CFSWITCH> is cleaner and much easier to follow.  I have heard that if the <CFIF> statements become lengthy and complex, and <CFSWITCH> could be used, that it would be wiser to do so.  I have heard that <CFIF> uses more memory and thus can cause a problem if there are a huge number of users.  Through my trials and testing, I have not seen performance changes in either direction.  I have tried to make sure that my <CFIF> statements are concatenated as much as possible.  Look at the following code:

<CFIF NOT request.qLabels.deleted>
..and..
<CFIF NOT request.qLabels.deleted EQ 1>

I have also heard from many that it is wiser to remove the 'EQ 1' whenever possible. Now if you are checking for an explicit value of '1', then this is fine. But this is specialized for boolean values. If you just want to know if the boolean is true or not, just leave out the 'EQ 1' and the server is more responsive. I tested this using a loop of over 10000 records, checking if it's deleted or not using both these forms, and when the 'EQ 1' was not there, there a much smaller server load and the response time was faster by about 15%. This is not a huge amount, but it was a performance gain, so I recommend removing operators if possible. Just remember, this can only be done with boolean type values. Another example would be to check if there are any records to display output, and this go as follows:

<CFIF request.qLabels.recordcount>
show desired output of records
<CFELSE>
show message explaining that there is no records in the query
</cfif>
Instead of... 
<CFIF request.qLabels.recordcount GT 0>
show desired output of records
<CFELSE>
show message explaining that there is no records in the query
</cfif>

These may seem like small things, but with large numbers of users and lots of server calls, there can be huge performance gains, but before I get too far off track, lets get to the verdict of our original topic here.

Verdict
So <CFSWITCH> is a better choice for areas that have one variable with many different value choices.  And, <CFIF> is better for all areas where a simple or complex function check on a variable is needed.

I tend to use <CFSWITCH> where I can, and since I like to design my apps in an MVC (model, view, controller) setup, it makes for easier debugging and inclusion of data.