Browse files

Rewriting the jQuery plugin to be more in line with normal jQuery met…

…hodology.
  • Loading branch information...
1 parent 367eb56 commit 17b324828fb476621012e21c8d5137801d2c0dfd @Zoramite Zoramite committed Apr 10, 2012
Showing with 56 additions and 56 deletions.
  1. 0 README
  2. +17 −0 README.md
  3. +39 −0 jquery.ziptastic.js
  4. +0 −56 zippidydoda.js
View
0 README
No changes.
View
17 README.md
@@ -0,0 +1,17 @@
+## Usage
+
+### Standalone lookup
+
+Can be used to query for a specific zip code.
+
+ $.ziptastic(48867, function(country, state, city, zip){
+ // Use the data...
+ })
+
+### Element Selector
+
+ $('#zip')
+ .ziptastic()
+ .on('zipChange', function(country, state, city, zip) {
+ // Use the data...
+ });
View
39 jquery.ziptastic.js
@@ -0,0 +1,39 @@
+(function( $ ) {
+ var requests = {};
+ var zipValid = {
+ us: /[0-9]{5}(-[0-9]{4})/
+ };
+
+ $.ziptastic = function(zip, callback){
+ // Only make unique requests
+ if(!requests[zip]) {
+ requests[zip] = $.getJSON('http://zip.elevenbasetwo.com?zip=' + zip);
+ }
+
+ // Bind to the finished request
+ requests[zip].done(function(data) {
+ callback(data.country, data.state, data.city, zip);
+ });
+
+ // Allow for binding to the deferred object
+ return requests[zip];
+ };
+
+ $.fn.ziptastic = function( options ) {
+ return this.each(function() {
+ var ele = $(this);
+
+ ele.on('input change', function() {
+ var zip = ele.val();
+
+ // TODO Non-US zip codes?
+ if(zipValid.us.test(zip)) {
+ $.ziptastic(zip, function(country, state, city) {
+ // Trigger the updated information
+ ele.trigger('zipChange', [country, state, city, zip]);
+ })
+ }
+ });
+ });
+ };
+})( jQuery );
View
56 zippidydoda.js
@@ -1,56 +0,0 @@
-(function( $ ) {
- $.fn.ziptastic = function( options ) {
-
- // Create some defaults, extending them with any options that were provided
- var settings = $.extend( {
- 'zip' : 'zip',
- 'cityid' : 'city',
- 'stateid' : 'state',
- 'countryid' : 'country'
- }, options);
-
- var zip_element = $("#" + settings.zipid);
-
- var city_element = $("#" + settings.cityid);
- var state_element = $("#" + settings.stateid);
- var country_element = $("#" + settings.countryid);
-
- // Hide the elements that we're going to retreive data for.
- city_element.parent().hide();
-
- state_element.parent().hide();
-
- country_element.parent().hide();
-
-
- $('#zip').change(function() {
- var client = new XMLHttpRequest();
- client.open("GET", "http://localhost?zip=" + this.value, true);
- client.onreadystatechange = function() {
- if(client.readyState == 4) {
- //alert(client.responseText);
- var location_data = JSON.parse(client.responseText);
-
- city_element.val(location_data.city);
- city_element.parent().show(500);
- state_element.val(location_data.state);
- state_element.parent().show(500);
- country_element.val(location_data.country);
- country_element.parent().show(500);
- };
- };
-
- client.send();
-
-
- });
-
- /*
-
-
- */
-
-
-
- };
-})( jQuery );

0 comments on commit 17b3248

Please sign in to comment.