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);