Updating a Spry Dataset from a Popup

Last week I posted a method of using multi-related selects with Spry and ColdFusion. In my example I updated a subbrands dataset when a brand select was updated. I implemented a similar process at work and decided that it would be nice to offer the option of adding new subbrands from the data entry form. To do so, I placed an ‘add’ button next to the subbrands select which opens a popup where the user can add a new subbrand. Then came the question, how do I update the dataset on the parent window once the new subbrand is added.

It actually ended up being quite easy.

The original code for defining the Spry datasets looked like this:

<script type="text/javascript">
var dsBrands = new Spry.Data.XMLDataSet("brands.cfm","brands/brand");
var dsSubbrands = new Spry.Data.XMLDataSet("subbrands.cfm?brandID={dsBrands::brandID}","subbrands/subbrand");
</script>

Once the subbrands for a particular brand were read in, however, they would no longer updated. To force an update I just had to add a little code and a function to the subbrands definition:

var dsSubbrands = new Spry.Data.XMLDataSet("xmlSubbrands.cfm?brand_id={dsBrands::brand_id}",
"subbrands/subbrand",{useCache:false});
function refreshSubbrands()  {dsSubbrands.url="xmlSubbrands.cfm?brand_id={dsBrands::brand_id}&cachebuster=<cfoutput>#Now()#</cfoutput>";
dsSubbrands.loadData();}

The first thing I do is disable the cache. Then I define a function that will redefine and reload the dsSubbrands dataset. I added in the date and time to the URL to further prevent XML caching. That’s all the code required to update the select list.

The only thing left to do from here is to get the popup to execute the function. This is accomplished by adding the following code after the insert is completed:

<script>
	if(window.opener) {
		window.opener.refreshSubbrands();
	}
</script>

window.opener is a reference to the window that opened the current window. The reason I check whether the referencing page exists is so that the add subbrand form can also be used as a normal page outside of the form that calls it as a popup. refreshSubbrands is the function I created to reload the subbrands select. In this way, the select list is updated as soon as the update is completed. No refresh is necessary, even if the parent brand is already selected.

Credit Where Credit Is Due
I found the guts of this technique on Andrew Powell’s ColdFusion, Mach-II, Flex, and Spry Blog where he has a great example demonstrating how to page through a set of data in 10 record increments.

-rG

ajax, coldFusion, spry

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

13 Responses to “Updating a Spry Dataset from a Popup”

Leave Comment

(required)

(required)