1

I'm just learning revealing module patterns today and I'm doing OK so far. However I'm wondering if when a revealing module pattern function is called from within another revealing module pattern, can you return a value to the caller pattern function?

For example I have a location module pattern that contains several functions, which I want another module pattern to use. Here is the code for it:

Location Revealing Module Pattern

// Revealing Modular pattern, containing functions relevant to location functionality    
var Location = (function(window, undefined){

    // Gets co-ordinates of users current position (TRUE: Run geoLocate function, FALSE: Run geoError function)
    function getCoords(){            
        navigator.geolocation.getCurrentPosition(getLocation, provideError);
    }

    // Get users current location
    function getLocation(position){

        var lat = position.coords.latitude;
        var lng = position.coords.longitude;

        // Convert seperate variables into one string type variable (Google geocode expects string format)
        var myLocation = String(lat+","+lng);

        console.log(myLocation);

    }

    // Error handler for geolocation
    function provideError(error){            
        console.log(error);
    }

    // Explicitly return public methods when this object is instantiated
    return{            
        getUserLocation : getCoords
    };

})(window);

Within my other revealing module pattern (below), I am calling the getUserLocation function (aka getCoords) and currently, it console.logs the users location fine (myLocation).

// Revealing Modular pattern, containing functions relevant to park functionality    
var Park = (function(window, undefined){

    // Determine if user entered postcode or selected 'use my location'
    function handleUserData(data){

        if(data === "postcode"){
            var location = document.getElementById("postcode").value;
        }
        else{
            var location = Location.getUserLocation();
        }

    }

    // Explicitly return public methods when this object is instantiated
    return{            
        handleMe : handleUserData
    };

})(window); 

However, when I change console.log(myLocation) to return myLocation and then console log location (in the function handleUserData), I get undefined.

My theory is that I want to get the users location and then use it within the Park revealing module pattern functions.

So my question is, is it possible to return a value from one revealing module pattern to another? If not, are there other methods I should research into that will help me with my question?

EDIT

From help in the comments, I was able to fix this issue using the following:

Location

// Revealing Modular pattern, containing functions relevant to location functionality    
var Location = (function(window, undefined){

    // Gets co-ordinates of users current position (TRUE: Run geoLocate function, FALSE: Run geoError function)
    function getCoords(callback){

        navigator.geolocation.getCurrentPosition(

            function(position){

                var lat = position.coords.latitude;
                var lng = position.coords.longitude;

                // Convert seperate variables into one string type variable (Google geocode expects string format)
                var myLocation = String(lat+","+lng);

                callback(myLocation);

            }

        )

    }

    // Explicitly return public methods when this object is instantiated
    return{            
        getUserLocation : getCoords
    };

})(window);

Park

// Revealing Modular pattern, containing functions relevant to park functionality    
var Park = (function(window, undefined){

    // Determine if user entered postcode or selected 'use my location'
    function handleUserData(data){

        var location;

        if(data === "postcode"){
            location = document.getElementById("postcode").value;
        }
        else{
            Location.getUserLocation(function(userLocation){
                location = userLocation;
                console.log(location);
            });
        }

    }

    // Explicitly return public methods when this object is instantiated
    return{            
        handleMe : handleUserData
    };

})(window);
  • this has nothing to do with the revealing module pattern. It's just that `getCurrentPosition` is asynchronous. Notice you're calling `getCoords`, so it won't help when you `return` from `getLocation`. – Bergi Apr 02 '15 at 15:45
  • When getCoords is called, if it runs successfully it calls the function 'getLocation'. Respectively if it fails, it then calls the function 'provideError'. It definitely is calling the getLocation function as in the code above, it states console.log(myLocation); and it does this fine. My problem is when I change that to return myLocation, when I try to receive that in the Park module pattern, I get undefined – DannyDanDan Apr 02 '15 at 15:49
  • Yes, because it calls `getLocation` **asynchronously** - "later", after `getCoords` has alread returned undefined! You cannot expect `getCoords` to return the return value of `getLocation`. – Bergi Apr 02 '15 at 15:53
  • Ah right I see, that makes sense then – DannyDanDan Apr 02 '15 at 15:58
  • Would you recommend executing both functions (getLocation and provideError) within getCoords and then returning the value? – DannyDanDan Apr 02 '15 at 16:03
  • 1
    http://stackoverflow.com/questions/20166039/js-geolocation-wait-until-success-before-return-value – david s Apr 02 '15 at 16:05
  • 1
    You cannot. `navigator.geolocation.getCurrentPosition` is asynchronous. That makes it absolutely impossible to return the value from `getCoords`. Use callbacks. – Bergi Apr 02 '15 at 16:07
  • Thanks guys. Using davids link as a reference, I've managed to get it working. I will edit my answer to show anybody else having this problem, how to resolve the issue – DannyDanDan Apr 02 '15 at 16:17

0 Answers0