Wednesday, July 30, 2014

Geolocation woes

Apparently HTML5 has an api to get me exactly where I am on the planet.  But it can't tell me what country I am in.  Google will tell me. Google knows all.

    this.getGeoLocation = function()
    {
        var deferred = $.Deferred();
        if(navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(function(position)
            {
                deferred.resolve(position);
                //position: {"coords":{"speed":null,"accuracy":65,"altitudeAccuracy":10,"altitude":1410.8349609375,"longitude":-111.72789187811416,"heading":null,"latitude":40.332329714635044},"timestamp":1406740874211}
            });
        }
        else
        {
            deferred.reject({error: "FAIL"});
        }
     return deferred.promise();
    };

Wednesday, July 2, 2014

Wait for it....

Deferred Objects with jquery.  Great for when you are waiting for data to process or servers to reply.

var deferredData = $.Deferred();
deferredData.done(function(result)
{
     //do stuff
}).fail(function(errorObj)
{
     //handel error
}).always(function(result)
{
     //whatever is returned comes here as well.
});

// when the data is done simply call
deferredData.resolve(result);

//or if you want to fail
deferredData.reject(errorObj);