Using ColdQuery

I have been using jQuery quite a bit lately and I really like the way it works. I found myself wanting some if it's features when I was working in ColdFusion, so I decided to try to emulate some of the more useful ones.

I have always found ColdFusion to be a little bit lacking when it comes to managing Arrays specifically for searching and sorting. jQuery does those things well in the context of the Browser DOM and I wanted to come up with something equivalent in CF.

First Sorting arrays of Components. i don't really know how others handle this, but it seems to come up for me fairly often. I will have an array of components and I need to display them on a page in a certain order. It's not a query so I can't use query of queries. Here is the syntax to do this in ColdQuery.

<cfscript>
myArray = myGateway.getAllMyObjectsAsArray();
cq = createObject("component","org.coldquery.ColdQuery")
.init(myArray);

//get items that start after the current time
cq = cq.$("function f(item){
return (dateCompare(now(),item.getStartDate()) GTE 0);
}"
);

//Sort by name and then by start date
cq = cq.sortByColumn("getName()","string","asc")
.sortByColumn("getStartDate()","date","asc");

//Save the modified array in a new variable
myNewArray = cq.getItems();
</cfscript>

Of course it's usually best to do these things in the database if you can, but sometimes I have found that I have these items loaded as aggregates of another object and I need to sort and filter them differently depending on the situation. For me this turns out to be a nice little shortcut wrapped up in a single Object.

I am also looking for any suggestions of useful methods to add.

Comments
Mary's Gravatar Dig your yellowpages,a wiki model english phrase dictionary. You can
make friends by sharing you favorites in http://www.duyp.net
http://www.diging.com
http://www.qxiu.com
http://www.qxiu.net
# Posted By Mary | 7/23/08 2:15 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.5.006.