mardi 7 mars 2017

Ember: Having trouble trying to use this.set to set a field with a variable declared inside an if statement

this is probably very easy for most you, but I really can't figure out what could be wrong with my code and I feel like I tried everything possible. Basically what I want to do, is to geocode a string adress, and store the result in my database. I managed to get the geocode part to work, as it sends back the latitude and longitude. Now, I would like to store the result back in my database. Usually I use this.set('myfield', myvar) but myvar is set inside an "if" and it looks like it prevent me to do that. What can I do ? Here is my code inside the "actions" section of my controller:

 geocode(location, mylat, mylng) {
  const google = window.google;
  var geocoder = new google.maps.Geocoder();
  var latlng = "";
  var lat = "";
  var lng = "";

 geocoder.geocode( { 'address': location }, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
        latlng = results[0].geometry.location;
           lat = latlng.lat();
          lng = latlng.lng();

           alert(lat); // it works
            alert(lng); // it works too
            this.set(`model.lat`, latlng); //not working here
}
}
);
alert(latlng); // not working
      this.set(`model.lat`, latlng); //not working too

},

I spent maybe 8 hours trying to solve this and I'm totally lost, so any help would be sincerely greatly appreciated. Many thanks in advance,




Aucun commentaire:

Enregistrer un commentaire