Is it possible to intercept Ember REST calls and / or cancel a promise?

I can usually write error handlers for rejected promises like this:

user.save().then(function() {
  // do stuff
}, function(reason) {
  user.rollback();
  _this.send('showModal', 'error');
});

      

Let's say I have another condition that could cause a promise to fail - for example, I know in advance that my application does not have an internet connection.

I can check before saving

if (this.get('connection.isOffline')) {
  this.send('showModal', 'disconnected')
} else {

  // proceed as normal...
  user.save().then(function() {
}

      

but I would rather not copy this code over and over.

One thought was to force all promises to reject if the app is disabled and then in the reject handlers I can check if the app is offline / offline and display a message accordingly.

Alternatively, I could intercept all Ember REST calls and check the connection first, abort the call, and make a message if the app is disconnected.

Which method is preferred? Is there another way?

+3


source to share


1 answer


I think your best bet is to extend / open on DS.Model and override .save () to test your connection. You will need to register your object / controller / anything with the DI container for it to be available in the base model or reopened by the DS.Model.

I'm not sure how your connection information is determined or at what point in your application it is available. But whatever object defines it, I would register it with the DI container via the application initializer and the container. () Inside. Then in your overridden save () run the command this.store.container.lookup ("type: name"). Get ('connection.isOffline') to check the connection status and do whatever. You can use this.store.container.lookup ("route: application"). Send ("offlineSaveAttempt") or return a rejected promise via DS.PromiseObject.create () so that everything agrees but doesn't run faster .. or whatever.



To deal with the find () function, you will need to extend / reopen on the store - if that is a requirement, you would probably be better off dealing with the store exclusively and not touching the DS.Model. The methods you might be interested in would be .scheduleSave () and .find (). You would do the same container search to find your object / controller / everything, but only this.container.lookup ()

+1


source







All Articles