Ask a Jedi: Updating data and refreshing with Spry

Mark from NZ had an interesting question about Spry:

I have a question regarding Spry, I have a simple dataset, one of the columns is a simple publish button, if on it displays a green image if off red etc. I was wondering if I can create an ajax call whenever the image is clicked it updates the database and dataset to show my changes. At the moment, when it is clicked it browses away from the page performs the update then a cflocation to take you back to show the changes which is a bit cumbersome.

Absolutely! First off - Spry can do calls back to the server for any number of purposes. Sending data back as a FORM post is relatively simple. First start off by reading:

Doing form posts in Spry

This will cover how you can send you data updates back to the server. How do you then refresh the current dataset? The Spry Dataset object has a loadData() method that - well - loads the data. One typical use case is to tweak the URL and reload the data:

dsData.url = "/index.cfm?event=xml.starbuckisntdead"; dsData.loadData();

loadData() by itself should be all you need, but if you run into IE caching issues than you could do the old random number in the URL trick.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for Extend by Auth0. He focuses on serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA