feat($on#event.preventDefault(promise)) optionally pass a promise into preventDefault
#5094
Description
Currently we can listen and cancel events such as $locationChangeStart
with the event
object's preventDefault
method.
What does everyone think of this proposed idea:
preventDefault - {function|Promise}: calling preventDefault sets defaultPrevented flag to true. Optionally pass a promise, when resolved continues the event. When rejected sets defaultPrevented flag to true.
defaultPrevented - {boolean}: true if preventDefault was called or it's promise rejected.
With this being implemented in Angular Core it would mean issues like angular-ui/ui-router#591 could be handled in a more decoupled way.
For example:
var $off = $rootScope.$on('$locationChangeStart', function(event) {
// Some user service that checks if the user is logged
// in to our system and sets up the appropriate services
// for our application.
var httpUserPromise = $http.get('/auth/user').catch(function() {
// User is not logged in, do our special handling just like we
// would if we were calling `preventDefault` sychronously.
});
event.preventDefault(httpUserPromise);
// Deregister listener so it is only called on
// first `$locationChangeStart` event.
$off();
});
This would give a wealth of deferred functionality for handling events allowing uses to asynchronously request application configuration in event listeners and allowing Angular to handle the event normally when resolved and in a custom manner when rejected.