Created
February 4, 2011 23:02
-
-
Save jcontonio/811966 to your computer and use it in GitHub Desktop.
jQuery based XML document loading and event handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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