Created
July 22, 2011 21:15
-
-
Save ericf/1100445 to your computer and use it in GitHub Desktop.
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
| // *** YQLSync *** // | |
| YQLSync = function(){}; | |
| YQLSync.prototype = { | |
| query : '', | |
| cache : new Y.CacheOffline, | |
| buildQuery : function () { | |
| return sub(this.query, { id: this.get('id') }); | |
| }, | |
| sync : function (action, options, callback) { | |
| if (action !== 'read') { return callback(null); } | |
| var query = this.buildQuery(options), | |
| cache = this.cache, | |
| results = cache.retrieve(query); | |
| // return cached results if we got ’em | |
| if (results) { return callback(null, results.response); } | |
| Y.YQL(query, function(r){ | |
| if (r.error) { | |
| callback(r.error, r); | |
| } else { | |
| results = r.query.results; | |
| cache.add(query, results); | |
| callback(null, results); | |
| } | |
| }); | |
| } | |
| }; | |
| // *** Place *** // | |
| Place = Y.Base.create('place', Y.Model, [YQLSync], { | |
| idAttribute : 'woeid', | |
| queries : { | |
| placeFromId : 'SELECT * FROM geo.places WHERE woeid={id}', | |
| placeFromLatLon : 'SELECT * FROM geo.places WHERE woeid in ' + | |
| '(SELECT place.woeid FROM flickr.places WHERE lat={latitude} AND lon={longitude})', | |
| placeFromPlaces : 'SELECT * FROM geo.places.common WHERE woeid1={woeid1} AND woeid2={woeid2}' | |
| }, | |
| buildQuery : function (options) { | |
| options || (options = {}); | |
| if (options.woeid1 && options.woeid2) { | |
| // load the place in common to these two places | |
| return sub(this.queries.placeFromPlaces, { | |
| woeid1 : woeid1, | |
| woeid2 : woeid2 | |
| }); | |
| } | |
| if (this.isNew()) { | |
| // assume we have a lat/lon | |
| return sub(this.queries.placeFromLatLon, { | |
| latitude : this.get('latitude'), | |
| longitude : this.get('longitude') | |
| }); | |
| } | |
| return sub(this.queries.placeFromId, { id: this.get('id') }); | |
| }, | |
| parse : function (results) { | |
| if ( ! results) { return; } | |
| var data = results.place, | |
| country = data.country, | |
| region = data.admin1, | |
| locality = data.locality1; | |
| return { | |
| woeid : data.woeid, | |
| centroid : data.centroid, | |
| boundingBox : data.boundingBox, | |
| country : country && country.content, | |
| region : region && region.content, | |
| locality : locality && locality.content | |
| }; | |
| }, | |
| toString : function () { | |
| var country = this.get('country'), | |
| region = this.get('region'), | |
| locality = this.get('locality'); | |
| if (locality) { return (locality + ', ' + region); } | |
| if (region) { return (region + ', ' + country); } | |
| return country || ''; | |
| } | |
| }, { | |
| ATTRS : { | |
| woeid : {}, | |
| country : {}, | |
| region : {}, | |
| locality : {}, | |
| latitude : {}, | |
| longitude : {} | |
| }, | |
| getCommonPlace : function (place1, place2, callback) { | |
| place1 instanceof Place || (place1 = new Place(place1)); | |
| place2 instanceof Place || (place2 = new Place(place2)); | |
| callback || (callback = function(){}); | |
| function loadCommonPlace () { | |
| var place = new Place().load({ | |
| woeid1 : place1.get('id'), | |
| woeid2 : place2.get('id') | |
| }, function(){ | |
| callback(place); | |
| }); | |
| } | |
| if (place1.isNew()) { | |
| place1.load(function(){ | |
| if (place2.isNew()) { | |
| place2.load(loadCommonPlace); | |
| } else { | |
| loadCommonPlace(); | |
| } | |
| }); | |
| return; | |
| } | |
| if (place2.isNew()) { | |
| place2.load(loadCommonPlace); | |
| return; | |
| } | |
| return loadCommonPlace(); | |
| } | |
| }); | |
| // Usage | |
| Place.getCommonPlace({ | |
| latitude : 42.35128040403806, | |
| longitude : -71.07214606708988 | |
| }, { | |
| latitude : 42.363965387785505, | |
| longitude : -71.05068839497073 | |
| }, function(place){ | |
| place.get('locality'); //=> Boston | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment