Skip to content

Instantly share code, notes, and snippets.

@jcontonio
Created February 4, 2011 23:02
Show Gist options
  • Save jcontonio/811966 to your computer and use it in GitHub Desktop.
Save jcontonio/811966 to your computer and use it in GitHub Desktop.
jQuery based XML document loading and event handling
/**
* Loads an XML document and broadcasts events on success or failure
* @author Jay Contonio
* @constructor
* @requires jQuery @link http://jquery.com/
* @param {String} xmlDocument The path to an XML document
*/
var XML = function(xmlDocument)
{
// Load the XML document, broadcasting an event on success of failure
var that = this;
$.ajax({
type: 'GET',
url: xmlDocument,
dataType: 'xml',
success: that.loadXMLCompleteHandler,
error: that.loadXMLErrorHandler
});
}
XML.prototype.loadXMLCompleteHandler = function(xmlData)
{
var successEvent = new jQuery.Event('XML_COMPLETED_LOAD');
successEvent.xml = xmlData;
successEvent.path = this.url;
$(document).trigger(successEvent);
}
XML.prototype.loadXMLErrorHandler = function(error)
{
throw new Error('XML error: ' + error.statusText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment