Using Deferreds to get location and show a map
<p>I'm attempting to understand the Revealing Module Pattern and jQuery Deferreds with a (what I thought was a) simple example.</p>

<p>What I want to be able to do is call a module to get the user's location and <em>when and if</em> that call succeeds show the location on a google map.</p>

<p>Both of these operations by themselves using a bunch of functions and callbacks are no problem for me, but I have attempted to do it in a more 'ravioli' rather than 'spaghetti' kind of way.  </p>

<p>What I'm finding is that while the deferred is working perfectly, my module's public properties do not contain the values that I have set during the operation to get the location.</p>

<p>This is my 'geoModule'</p>

<pre><code>var geoModule = function () {
    var lat;
    var lng;

    var init = function () {
    return $.Deferred(function (def) {
        getCurrentPositionDeferred({})
        .fail(function () { def.reject(); })
        .done(function (location) {
            lat = location.coords.latitude;
            lng = location.coords.longitude;
            logger.log("Got location : " + lat + " : " + lng);
            def.resolve();
        });
    }).promise();
    };

    var getCurrentPositionDeferred = function(options) {
    var deferred = $.Deferred();

    navigator.geolocation.getCurrentPosition(deferred.resolve, deferred.reject, options);

    return deferred.promise();
    };

    return { lat: lat, lng: lng, init: init };
}();
</code></pre>

<p>and this is my 'mapModule'</p>

<pre><code>var mapModule = function (mapId) {
    var mapElement = document.getElementById(mapId);
    var map;
    var initMap = function (lat, lng) {
    logger.log("init map: " +lat + " : " + lng);
    var myOptions = {
        zoom: 14,
        center: new google.maps.LatLng(lat, lng),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(mapElement, myOptions);
    };


    return { initMap: initMap };
}('map');
</code></pre>

<p>When I bring them together like this:</p>

<pre><code>$.when(geoModule.init())
    .done(function () {
        logger.log("location done " + geoModule.lat + " : " + geoModule.lng);
        mapModule.initMap(geoModule.lat, geoModule.lng);
    });
</code></pre>

<p>the geoModule.lat and geoModule.lng are undefined, even thought the code to set them in geoLocation is running successfully.</p>

<p>I've created a full example at <a href="http://jsfiddle.net/faG4J/4/" rel="nofollow">http://jsfiddle.net/faG4J/4/</a> that shows the logging output.  I'm probably doing something really stupid, so any help would be most appreciated.</p>
