I have a route which returns 4 promises wrapped inside an JS object. One of them is just a query to the model, but the other 3 are ajax requests.
I know that I can use Ember.RSVP.hash, but I want to be able to show the route’s template, even if one of the promises hasn’t been resolved or has been rejected.
The problem is that Route’s substate events (loading/error) are not called when I return the promises wrapped inside the Javascript object as follows:
If you wan to return promises even if one fails you could manage it manually. You can wrap the thing in a promise and handle the success and failure and return what you want.
Thanks @varblob, I will try that. However, I think that even using hashSettled, I won’t be able to show any resolved promise until all promises are fulfilled or rejected. On my example, each promise’s data could be displayed as it’s resolved… but loading/error events are not called.
@amk do loading/error events get called when a promise is rejected (404, etc)?
Yeah, then you’d probably need to work up something on your own. An option would be to build an “aggregate” promise that resolves when any model resolves or rejects when all models have rejected. This would let you still use loading/error states according to the behavior you’re looking for. That doesn’t sound very hard, but it isn’t something available off the shelf in RSVP.
The problem there is that you’d probably still have to work with promises at the controller/component/template level, but that seems pretty much inevitable for this design.
I think I misunderstood your intentions. If you want to handle the promises loading state after the route has transitioned, ie don’t block the transition waiting for promises to resolve then you were on the correct path with a plain javascript object.
If you want to fire events based on how the promises are proceeding you will have to do so manually. Maybe something like this, you could even write a function that runs through a js object and promise watches each then you can do stuff like check if everything is done loading:
You’ll have to play with the watch function as I’m not sure if you need to wrap it in a promise to call .then and .catch on or if you can just return something in the then( function(){ _this.send('doneLoading')
@amk@awj@varblob thanks for all your examples. They’ve been pretty helpful.
The reason why I’m trying to go with this design, it’s because my route is a dashboard with statistics and charts retrieved from different API’s endpoints. So, I wanted to start showing info to the user as soon as something was available. Error event handling is necessary whether you request for a nonexistent user (404) or any other reason causing the promises to reject.
Yes, but for now you can use them. As soon as that RFC gets implemented (Ember 2.2 ?) it will require few minutes to refactor this, since new attributes API can simulate after/before hooks pretty easily
@H1D@varblob yes, alsosetupController (suggested above) will be deprecated. Thanks for your comments, I didn’t know that Model hook won’t be called in some situations.
We are currently working on a similar system for our application, and looking for a way to display loading states independent of the other promises resolving or rejecting. What we decided on is injecting the store into components, and then handling loading, error and loaded states by hand.
Each component has the same setup data that they all need before they can proceed, in our case the user, so that is loaded in the route, and then passed into each component. Then the components query the server independently.
This way we can filter data within components, or switch out components without having to re-enter the model hook and re-query everything.
While this very much seems like an anti pattern, it seems like the least bad way to do this.