1

Below is the code example. This is for addon panel.

The router promise does not get resolved from inside global listner, though it works with normal ajax requests.

import Ember from 'ember';

export default Ember.Route.extend({


  model: function() {
  return new Ember.RSVP.Promise(function(resolve, reject) {

// This works 
Ember.$.ajax({
    url : "http://bla.com"
    type: "POST"
    }).then(function(response){
        resolve(response);;
    });


// This doesn't work
addon.port.on(url, function(status, response) {
    resolve(response); 
})



  });
});

I read somewhere this can be handled with Ember.run.bind but couldn't get it to work.

Ritvick
  • 78
  • 6
  • Does it work with asynchronous functions that are not from Ember (e.g. native XHR or `setTimeout)`? – Bergi Sep 20 '15 at 15:01
  • Probably not. from http://stackoverflow.com/questions/20602692/ember-return-a-promise-from-beforemodel-not-working-with-qunit and http://stackoverflow.com/questions/22685055/rsvp-handling-timeouts-with-promises – Ritvick Sep 20 '15 at 15:09

1 Answers1

0

Does it work if you Ember.run.join(this, resolve, response) ?

addon.port.on(url, function(status, response) {
    // just in case it doesn't work, add a log here to make sure your callback
    // is actually being called.
    console.log('got response');
    Ember.run.join(this, resolve, response); 
});

Ember.run.join will join an existing run loop or create a new one.

gnarf
  • 105,192
  • 25
  • 127
  • 161
  • It give me this error Message: TypeError: this.get(...).toArray is not a function. "response" is an array [] in callback. – Ritvick Sep 20 '15 at 16:47
  • It looks like the promise is not waiting for getting fulfilled, and thus the model is not properly formed. Is there a way we can tell ember to wait for message on addon.port.on() ? – Ritvick Sep 21 '15 at 03:26