Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($on#event.preventDefault(promise)) optionally pass a promise into preventDefault #5094

Open
@rynz

Description

@rynz

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions