Created
October 17, 2011 22:06
-
-
Save leadVisionary/1293982 to your computer and use it in GitHub Desktop.
Typical jQuery versus Better jQuery
This file contains 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
jQuery(document).ready(function(){ | |
jQuery("#zipCode").change(function(){ | |
jQuery.ajax({ | |
type : 'POST', | |
dataType : 'json', | |
data : { | |
zipCode : jQuery(this).val(), | |
state : jQuery("#state").val() | |
}, | |
url : <g:link action="territoryLookup" />, | |
success : function(data, textStatus, jqXHR) { | |
jQuery.each(data.territories, function(index, value) { | |
var selectBox = jQuery("#territories"); | |
jQuery("<option>",{ value : value }).appendTo(selectBox); | |
}); | |
jQuery.each(data.cities, function(index, value) { | |
var selectBox = jQuery("#cities"); | |
jQuery("<option>",{ value : value }).appendTo(selectBox); | |
}); | |
jQuery.each(data.counties, function(index, value) { | |
var selectBox = jQuery("#counties"); | |
jQuery("<option>",{ value : value }).appendTo(selectBox); | |
}); | |
}, | |
error:function(XMLHttpRequest,textStatus,errorThrown) { | |
var flash = jQuery("#flashMessage"); | |
if(flash.length == 0) { | |
jQuery("<div />", { | |
"class" : "message errors", | |
id : "flashMessage" | |
}).appendTo("#bannerLogo"); | |
} | |
var message = errorThrown + ": " + jqXHR.responseText | |
jQuery("#flashMessage").text(message); | |
} | |
}); | |
}); | |
}); |
This file contains 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
jQuery(document).ready(function(){ | |
registerEventListeners(); | |
}); | |
function registerEventListeners(){ | |
jQuery("#zipCode").change(territoryLookup); | |
} | |
function territoryLookup(){ | |
jQuery.ajax({ | |
type : 'POST', | |
dataType : 'json', | |
data : { | |
zipCode : jQuery("#zipCode").val(), | |
state : jQuery("#state").val() | |
}, | |
url : "<g:createLink action="territoryLookup" />", | |
success : populateTerritoryInformation, | |
error: handleErrors | |
}); | |
} | |
function populateTerritoryInformation(data, textStatus, jqXHR){ | |
var infoPieces = ["cities", "counties", "territories"] | |
for(var piece in infoPieces) | |
{ | |
var selector = "#" + piece | |
var dataElement = data[piece] | |
createSelectOptions(dataElement, jQuery(selector)); | |
} | |
} | |
function createSelectOptions(jsonObject, select) { | |
jQuery.each(jsonObject, function(index, value) { | |
addOption(select, value); | |
}); | |
} | |
function addOption(select, newValue) { | |
var option = jQuery("<option>",{ value : newValue }).appendTo(select); | |
} | |
function handleErrors(jqXHR,textStatus,errorThrown){ | |
createFlash(); | |
var message = errorThrown + ": " + jqXHR.responseText | |
jQuery("#flashMessage").text(message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment