You need to sign in to do that
Don't have an account?
setCallBack between 2 functions present in JS Helper
I have 2 functions in my JS helper - getLatLng and getResponse. getLatLng gets the current location of the user and sets two attributes in the component namely lat and lng. getResponse uses these two attributes for further processing. I am calling both these functions from my JS controller one after the other. The problem is that getResponse gets called even before the execution of getLatLng is completed. Is there any way to set a callback so that when getLatLng sets the lat and lng on component, then only getResponse is called? Here is the code for JS controller and JS helper.
WeatherController.js
WeatherController.js
({ doInit : function(component, event, helper) { helper.getLatLng(component); helper.getResponse(component); } })WeatherHelper.js
({ getResponse : function(component){ var action = component.get("c.getCalloutResponseContents"); var lat = component.get("v.lat"); var lng = component.get("v.lng"); action.setParams({ "url": 'http://api.openweathermap.org/data/2.5/weather?lat=' +lat+ '&lon=' +lng+ '&appid=9b2719bd93477d05ad2575ccb81cb666&units=metric' }); action.setCallback(this, function(response){ var state = response.getState(); if (component.isValid() && state === "SUCCESS") { component.set("v.response", JSON.parse(response.getReturnValue())); } }); $A.enqueueAction(action); }, getLatLng : function(component) { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { component.set("v.lat", position.coords.latitude); component.set("v.lng", position.coords.longitude); }); } } })Thanks in advance for your help.
i update your code try below code
WeatherHelper.js
LatLngHelper.js
we fire the event when the response is come back from server for navigator.geolocation.getCurrentPosition
output -:
thanks let me inform if it helps you :)
sfdcmonkey.com
All Answers
Thanks for your reply. I am trying to optimize the code a bit and have broken down Weather component into two components.
Weather.cmp
WeatherController.js
WeatherHelper.js
LatLng.cmp
LatLngController.js
LatLngHelper.js
latLngEvent.evt
WeatherApp.app
The issue that I am facing now is in WeatherHelper.js, the console.log is printing the value of lat as undefined. Any idea why the value is not coming? Thanks for your help.
yes i find this issue its heppen because lightning is async freamwork so its not wait for complete the navigator.geolocation.getCurrentPosition function so you getting null value, anyway i find any other way for do it and i post here ASAP
thanks
i update your code try below code
WeatherHelper.js
LatLngHelper.js
we fire the event when the response is come back from server for navigator.geolocation.getCurrentPosition
output -:
thanks let me inform if it helps you :)
sfdcmonkey.com
You are a champ. I changed the way I was fetching parameters from components (changed v.lat to lat as suggested by you) and its working now. Thanks a lot.