the singularity of being and nothingness
Why It's Important to Read the Docs…
This morning, I was working on a bit of code for a dynamic directory search that I am building for work. Because I like the paging options that Spry provides, I decided to wire up Spry to ColdFusion to create some datasets. No big deal.
However, I came across an issue I have not encountered with Spry before. Normally, in the master/detail/filter mode of Spry dataset work, I will load a dataset from ColdFusion on page load and then use Spry to filter these results. The filter parameters normally come from the dataset itself.
For example, here's a run of the mill dataset:
This will get all the people in my database from the query results of my ColdFusion component. If I wanted to see a detailed view of one of the people, I would simply pass a Spry dataset value (e.g., '@id') to this dataset, and it would handle it nicely.
While this works fine for a small number of records, what if we're talking about tens of thousands? I'm actually not sure how long it would take for Spry to deal with, say, 10,000 personal information records. However, I suspect it would be an arduous task for Spry. Therefore, it is probably better that the records be filtered before being turned into a Spry dataset.
So how would this happen asynchronously? Obviously, I would want to pass an argument to my ColdFusion component. It could look something like this:
At first, I thought this would be simple. I could simply var scope my "criteria" variable above the dataset creation and then overwrite it when I update my recordset set. Here's what I tried:
if(!document.getElementById('filterTF')) {
var criteria = 'null';
}
else {
var criteria = document.getElementById('filterTF').value;
}
return criteria;
}
var dsPeople = new Spry.Data.XMLDataSet("com/getpeople.cfc?method=getPeople&criteria=" + getCriteria(), "/directory/person");
function getPeople() {
dsPeople.loadData();
}
Now obviously this did not work because the reloading of the Spry dataset does not recreate the dataset per se. My problem centered around the criteria variable–the Spry dataset was not recognizing that "criteria" had changed. I wrangled with about a dozen versions of this block of code, to no avail.
Finally, I dejected turned to the documentation. After looking for about 3 minutes, I ran across the promising setURL() function. Basically, setURL() allows you to change the url from which the dataset is being created without defining an entirely new dataset.
This looked promising. In my getPeople() function, I added the following:
In short, in this function call I am pointing Spry to the same ColdFusion component with the returned result of the getCriteria() function. However, in this scenario, the adjusted value of "criteria" is recognized and applied to the subsequent loadData().
It worked like a charm. Now, instead of having to bring in a buttload of records just so that I can filter them, I can now filter before the data ever hits Spry, and use Spry's dataset features for other purposes.
This does bring up an interesting issue, however: While it might take a bit of time to load a 10,000 record dataset into Spry's memory, the option I have outlined above requires numerous database hits to accomplish the same thing. I wonder which is more time and resource consuming, ultimately…
So that's about it. If you're interested, here is the final code:
if(!document.getElementById('myfilterfield')) {
var criteria = 'null';
}
else {
var criteria = document.getElementById('myfilterfield').value;
}
return criteria;
}
var dsPeople = new Spry.Data.XMLDataSet("com/getpeople.cfc?method=getPeople&criteria=" + getCriteria(), "/directory/person");
function getPeople() {
dsPeople.setURL("com/getpeople.cfc?method=getPeople&criteria=" + getCriteria());
dsPeople.loadData();
}
Print article | This entry was posted by existdissolve on December 20, 2007 at 11:03 am, and is filed under Spry Framework. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |