No way to hook into error events globally
See original GitHub issueRight now there is no reliable way to hook into outgoing requests (for something like a loading animation, or disabling a save button) because it isn’t possible to hook into failed XHR which is required to update state when the request finishes, albeit with a failure.
I can do something like this (via angular):
.run(['DS', '$rootScope', function(DS, $rootScope){
angular.extend(DS.defaults, {
beforeCreate: function (Resource, instance, cb) {
$rootScope.$evalAsync(function(){
instance.$$creating = true;
cb(null, instance);
});
},
afterCreate: function (Resource, instance, cb) {
$rootScope.$evalAsync(function(){
instance.$$creating = false;
cb(null, instance);
});
},
beforeUpdate: function (Resource, instance, cb) {
$rootScope.$evalAsync(function(){
instance.$$updating = true;
cb(null, instance);
});
},
afterUpdate: function (Resource, instance, cb) {
$rootScope.$evalAsync(function(){
instance.$$updating = false;
cb(null, instance);
});
},
beforeDestroy: function (Resource, instance, cb) {
$rootScope.$evalAsync(function(){
instance.$$destroying = true;
cb(null, instance);
});
},
afterDestroy: function (Resource, instance, cb) {
$rootScope.$evalAsync(function(){
instance.$$destroying = false;
cb(null, instance);
});
}
});
}])
But this doesn’t work if an error occurs, because the instance.$$<action>
property doesn’t get set back to false when a request fails (like with a 401). Any thoughts on how we should implement this? I was thinking something along the lines of:
DS#afterCreateError
DS#afterUpdateError
DS#afterDestroyError
This would avoid breaking changes, that could POSSIBLY be made if we simply call DS#after<action>
with errors, passing the error object in as a 4th param?
Let me know what you think @jmdobry . I am down to submit a PR for either case.
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (5 by maintainers)
@crobinson42 first post in this issue has pretty much a detailed explanation, but to make it short, I would like to be able to do something like:
**note: **
after<Action>
hooks don’t fire on errorsFor now it can be worked around on Adapter level using it’s
responseError
like this:This perfectly fits in what I need, but would be nice to have something like this out of the box in core functionality, probably on Mapper level.
Do you have a code example or more details?