Skip to content

$document event listener causes memory leak #84

Description

@acidaris

Found this when attempting to debug crashes of javascript tests.

This click event added to the $document is never cleaned up. This means any page that renders a angular-pickadate component will leak memory, especially if the application uses ui router that might render many pickadate components.

see https://github.com/restorando/angular-pickadate/blob/master/src/angular-pickadate.js#L82

Should instead be something like this

return function(scope, element, rootNode) {
        var togglePicker = function(toggle) {
          scope.displayPicker = toggle;
          scope.$apply();
        };

        var handleDocumentClick = function(e) function(e) {
          if (isDescendant(rootNode, e.target) || e.target === element[0]) return;
          togglePicker(false);
        }

        element.on('focus', function() {
          scope.modalStyles = computeStyles(element[0]);
          togglePicker(true);
        });

        element.on('keydown', function(e) {
          if (indexOf.call([9, 13, 27], e.keyCode) >= 0) togglePicker(false);
        });

        $document.on('click', handleDocumentClick);

        // Remove click event when element is destroyed.
        element.on('$destroy', function(){
             $document.off('click, handleDocumentClick);
       });

      };

    }])

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions