For a specific service call, I'm using jQuery instead of ember data:
return Ember.$.get(url).then((json) => {
this.store.pushPayload('user', json);
return this.store.getById('user', json.user.id);
});
This breaks ember's testing:
Uncaught Error: Assertion Failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run
I can fix this by adding a run inside my promise's .then function:
return Ember.$.get(url).then((json) => {
Ember.run(() => {
this.store.pushPayload('user', json);
return this.store.getById('user', json.user.id);
})
});
This doesn't work for me though, because Ember.run(..) doesn't return anything, so my promise resolves to undefined. I've seen some suggestions to move Ember.run out one level so it wraps the method returning the promise instead. This will then return the correct value, but the tests will continue to fail.
What's the proper way to return a promise that resolves to a value, while not breaking tests?
Thanks!
Edit 1
I did get it working with this code:
var self = this;
return Ember.$.get(url).then((json) => {
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.run(function() {
self.store.pushPayload('user', json);
var user = self.store.getById('user', json.user.id);
debugger;
resolve(user);
});
});
});
Is this the right way to do it? It feels like a lot of added complexity just to do an ajax call.