Fixing AJAX edit-in-place errors with object tests

I’ve been using Joseph Scott’s Edit-in-place (EIP) routine in the Topic Map Kid to help with modifying the various pieces of metadata in topics.

The problem comes when you start to use the same one file to apply to different to different topics with different editable fields. What I kept getting as a result was a set of errors when the layer to be edit-in-placeable wasn’t there.

I decided that the best way to work-around this issue was to just check to see if the layers existed before initialising them:

function testForObject(Id, Tag) {
  var o = document.getElementById(Id);
  if (o)  {
    if (Tag) {
      if (o.tagName.toLowerCase() == Tag.toLowerCase()) {
        return o;
      }
    }
    else {
      return o;
    }
  }
  return null;
}
Event.observe(window, 'load', init, false);
function init() {
	var topicID = document.getElementById('itemID');
	var topicIDstr = topicID.innerHTML;
	EditInPlace.defaults['type'] = ‘text’;
	EditInPlace.defaults['save_url'] = ’some URL’;
	var o = testForObject(”heading1″);
	if (o) {
		EditInPlace.makeEditable({
		  id: ‘heading1′,
		  ajax_data: {
		    api: ‘h1′,
		    itemID: topicIDstr
		  }
		});
	}
	var o = testForObject(”gift-price”);
	if (o) {
		EditInPlace.makeEditable({
		  id: ‘gift-price’,
		  ajax_data: {
		    api: ‘changeGiftPrice’,
		    itemID: topicIDstr
		  }
		});
	}

}

If you plop this into your file then you won’t have those pesky javascript errors.M

Leave a Reply