{"action":"closed","issue":{"url":"https://api.github.com/repos/ruhley/angular-color-picker/issues/30","labels_url":"https://api.github.com/repos/ruhley/angular-color-picker/issues/30/labels{/name}","comments_url":"https://api.github.com/repos/ruhley/angular-color-picker/issues/30/comments","events_url":"https://api.github.com/repos/ruhley/angular-color-picker/issues/30/events","html_url":"https://github.com/ruhley/angular-color-picker/issues/30","id":107948792,"number":30,"title":"$rootScope.$digest overwhelming calls","user":{"login":"FarSeeing","id":559569,"avatar_url":"https://avatars.githubusercontent.com/u/559569?v=3","gravatar_id":"","url":"https://api.github.com/users/FarSeeing","html_url":"https://github.com/FarSeeing","followers_url":"https://api.github.com/users/FarSeeing/followers","following_url":"https://api.github.com/users/FarSeeing/following{/other_user}","gists_url":"https://api.github.com/users/FarSeeing/gists{/gist_id}","starred_url":"https://api.github.com/users/FarSeeing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/FarSeeing/subscriptions","organizations_url":"https://api.github.com/users/FarSeeing/orgs","repos_url":"https://api.github.com/users/FarSeeing/repos","events_url":"https://api.github.com/users/FarSeeing/events{/privacy}","received_events_url":"https://api.github.com/users/FarSeeing/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ruhley/angular-color-picker/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/ruhley/angular-color-picker/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"closed","locked":false,"assignee":{"login":"ruhley","id":970772,"avatar_url":"https://avatars.githubusercontent.com/u/970772?v=3","gravatar_id":"","url":"https://api.github.com/users/ruhley","html_url":"https://github.com/ruhley","followers_url":"https://api.github.com/users/ruhley/followers","following_url":"https://api.github.com/users/ruhley/following{/other_user}","gists_url":"https://api.github.com/users/ruhley/gists{/gist_id}","starred_url":"https://api.github.com/users/ruhley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ruhley/subscriptions","organizations_url":"https://api.github.com/users/ruhley/orgs","repos_url":"https://api.github.com/users/ruhley/repos","events_url":"https://api.github.com/users/ruhley/events{/privacy}","received_events_url":"https://api.github.com/users/ruhley/received_events","type":"User","site_admin":false},"milestone":null,"comments":2,"created_at":"2015-09-23T15:42:21Z","updated_at":"2015-09-28T22:20:55Z","closed_at":"2015-09-28T22:20:55Z","body":"We have a problem of numerous `$rootScope.$digest` calls that results of calling all watchers of all scopes.\r\nThis 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:\r\n```javascript\r\n$document.on('click', function (evt) {\r\n    if ($scope.find(evt.target).length === 0) {\r\n        $scope.log('Color Picker: Document Hide Event');\r\n        $scope.hide();\r\n    }\r\n});\r\n$scope.hide = function (apply) {\r\n    $scope.log('Color Picker: Hide Event');\r\n    $scope.visible = false;\r\n\r\n    if (apply !== false) {\r\n        $scope.$apply();\r\n    }\r\n};\r\n```\r\n\r\nWhat I see that could be improved:\r\n* check if `$scope.visible` is `true` and exit early otherwise,\r\n* rewrite `$scope.contains` to use native [contains](https://developer.mozilla.org/en-US/docs/Web/API/Node/contains) method. Check for [performance test](http://jsperf.com/contains-native-vs-jquery/2)\r\n* get rid of calling `$scope.$apply()`?\r\n* handle `$destroy` event to remove click event listeners,\r\n* store references to all instances and call event handler only once?\r\n\r\nThank you."}}