Wednesday, March 16, 2011

ZoneUpdater - jQuery edition

I recently started using the excellent tapestry-jquery plugin that replaces prototype in T5. The internal implementation of zones in tapestry changes quite a bit in tapestry-jquery, so I had to change a couple of things in ZoneUpdater too. I find the updated code to be quite a bit better, here it is:



var ZoneUpdater = function(spec) {

var elementId = spec.elementId;
var element = document.getElementById(elementId);
var url = spec.url;
var $zone = jQuery('#' + spec.zone);

var updateZone = function() {
var updatedUrl = url;
var params = {};
if (element.value) {
params.param = element.value;
}
$zone.tapestryZone('update', {
url : updatedUrl,
params : params
});
}

if (spec.event) {
var event = spec.event;
jQuery(element).bind(event, updateZone);
}


return {
updateZone : updateZone
};
}