vendredi 29 avril 2016

Promise result in Ember Data computed property

I'm trying to make a call to an external API and use the results as a computed property in my Ember Data model. The result is fetched fine, but the computed property returns before the Promise resolves, resulting in undefined. Is this a use case for an Observer?

address = Ember.computed('lat', 'lng', function() {
  var url = `http://ift.tt/1SDrXGA}`;
  var addr;

  var request = new Ember.RSVP.Promise(function(resolve, reject) {             
      Ember.$.ajax(url, {                                                        
        success: function(response) {                                            
          resolve(response);                                                     
        },                                                                       
        error: function(reason) {                                                
          reject(reason);                                                        
        }                                                                        
      });                                                                        
   });                                                                          

   request.then(function(response) {                      
     addr = response.results[0].formatted_address;                              
   }, function(error) {                                                         
     console.log(error);
   })  

   return addr;
})




Aucun commentaire:

Enregistrer un commentaire