ruhley / angular-color-picker

Vanilla AngularJS Color Picker Directive with no requirement on jQuery
http://ruhley.github.io/angular-color-picker/
MIT License
165 stars 78 forks source link

$rootScope.$digest overwhelming calls #30

Closed FarSeeing closed 9 years ago

FarSeeing commented 9 years ago

We have a problem of numerous $rootScope.$digest calls that results of calling all watchers of all scopes. This happens because we have several instances of color picker on the same page, each one of them adding an event handler for all clicks on the whole document:

$document.on('click', function (evt) {
    if ($scope.find(evt.target).length === 0) {
        $scope.log('Color Picker: Document Hide Event');
        $scope.hide();
    }
});
$scope.hide = function (apply) {
    $scope.log('Color Picker: Hide Event');
    $scope.visible = false;

    if (apply !== false) {
        $scope.$apply();
    }
};

What I see that could be improved:

Thank you.

ruhley commented 9 years ago

Hi @FarSeeing,

Thanks for looking through the code and finding some improvements. I have implemented some of your suggestions in commit c0a488f0ab8d18afea653f24e371c2d14bbad2ad and released in v0.6.8. I did a slightly premature release to get some other bug fixes in for other people and I am currently pressed for time.

The improvements I have made should drastically reduce the number of $digest calls. Hopefully this will fix your problems. If so, can you close the ticket, otherwise I will implement them when I get the time.

FarSeeing commented 9 years ago

Thanks for your reply. You are right about $scope.$apply - I missed that click event listener is fired from non-Angular environment. So there's no other way but to call $scope.$apply. I guess this issue is solved unless some new ideas arise. Thank you.