Reputation: 27455
I'm putting stuff into a queue and processing them one by one. The stuff may belong to an AngularJS scope. If the scope it belongs to has been already destroyed by the time I get to it, I want to skip processing it. (The user has navigated away from the UI that would display the result, so there is no point computing it.)
One option is this:
function enqueue(stuff, scope) {
queue.push(stuff);
scope.$on('$destroy', function() {
queue.discard(stuff);
});
}
But I feel like this is somewhat heavy-weight. It adds an event handler for each "stuff". (I would also need to remove this event handler when I dequeue the item.) I feel it would be better to check if the scope has been destroyed at the time when I dequeue an item.
function process(stuff, scope) {
if (scope.$destroyed) { // Fictional API.
return; // Skip processing, scope is already gone.
} else {
... // Actually process "stuff".
}
}
scope.$destroyed
is made up, but I'm looking for something similar. Thanks!
Upvotes: 2
Views: 2261
Reputation: 132
You can use $scope.$$destroyed
flag to check if the scope is destroyed.
Upvotes: 2