angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Support multiple expressions in a single watch

Open jonbcard opened this issue 13 years ago • 9 comments

It would be awesome to be able to support multiple expressions in a single $watch. e.g.

$scope.firstName = 'John';
$scope.lastName 'Doe';
$scope.$watch(['firstName', 'lastName'], function(newVal, oldVal){
    $scope.fullName = newVal[0] + newVal[1];
}

or something of the sort.

This would provide a nice way to only re-evaluate computationally expensive functions when we know the result could be different.

jonbcard avatar Dec 06 '12 00:12 jonbcard

Bump

cbier avatar Jan 31 '13 17:01 cbier

Since $watch evaluates any valid expression, and compares it to the value last time, there is nothing stopping you doing this, you just have to change your syntax a little:

  $scope.$watch('[firstName, lastName]', function(newVal, oldVal) { ... });

And as a bonus, two less ' marks :smile:

[readyToClose]?

latentflip avatar Jan 31 '13 17:01 latentflip

Have you tested that @latentflip ? I've never seen anything to suggest that would be a supported expression, and my 30 second test throws an error.

jonbcard avatar Jan 31 '13 18:01 jonbcard

Yeah, $watch just takes any old expression. The requirements are: it's idempotent, i.e. you wouldn't want to use Math.random() or a Date in it, as it would always change. And that it's fast: you don't want it to be slow as it's run a lot.

Otherwise no reason it shouldn't work.

Made you a plnkr to demonstrate. http://plnkr.co/edit/LktbjBW8skaiMomW8Ljc?p=preview

latentflip avatar Jan 31 '13 18:01 latentflip

@latentflip : Hmmm.. Thanks :). That works if you add true as the third parameter of the $watch -- right now it gets stuck in a $digest loop -- in the same way that this would work:

scope.$watch(function(){ return [$scope.firstName, $scope.lastName]; }, doStuff, true);

I still don't think this is the same as a multi-parameter watch, though. Sorry about all the spam on this ticket :).

jonbcard avatar Jan 31 '13 18:01 jonbcard

You're right, my bad, forgot that pesky true ;) Don't worry, I don't think it's spam, we need to scope out the use case if it's going to make it in.

What do you dislike about the above? Having to remember to add true? Because I suspect any implementation of a multi-parameter watch is either going to effectively do that, or bind two separate watchers.

latentflip avatar Jan 31 '13 18:01 latentflip

@latentflip : The real use-cases don't really shine through in my simplified example. While the example you provided is probably still useful for a number of cases, the main problem with switching the object equality flag on and doing things this way is that it will often create inefficient watches as soon as we're not dealing with primitives anymore (since it has to do deep checks for equality).

I agree one easy implementation of this would probably be to bind multiple watches then make sure they only fire once. This is how I'm doing it in my own code: http://plnkr.co/edit/PfH8mX?p=preview

Just to provide a bit more context on the issue, here is the mailing list thread where @IgorMinar suggested that this ticket be opened: https://groups.google.com/d/msg/angular/bh5na_FxQ7g/Xvk6FTkgItMJ

jonbcard avatar Jan 31 '13 20:01 jonbcard

Oh my bad, sorry, had missed that discussion.

Just trying to help out with issue triage, but looks like it has been triaged already :smile:

As you were :smile: On 31 Jan 2013 20:31, "Jon Card" [email protected] wrote:

@latentflip https://github.com/latentflip : The real use-cases don't really shine through in my simplified example. While the example you provided is probably still useful for a number of cases, the main problem with switching the object equality flag on and doing things this way is that it will often create inefficient watches as soon as we're not dealing with primitives anymore (since it has to do deep checks for equality).

I agree one easy implementation of this would probably be to bind multiple watches then make sure they only fire once. This is how I'm doing it in my own code: http://plnkr.co/edit/PfH8mX?p=preview

Just to provide a bit more context on the issue, here is the mailing list thread where @IgorMinar https://github.com/IgorMinar suggested that this ticket be opened: https://groups.google.com/d/msg/angular/bh5na_FxQ7g/Xvk6FTkgItMJ

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1659#issuecomment-12964057.

latentflip avatar Jan 31 '13 20:01 latentflip

The new $watchCollection function added by https://github.com/angular/angular.js/commit/5eb968553a1130461ab8704535691e00eb154ac2 seems to handle this just dandy with the syntax:

$scope.$watchCollection('[firstName, lastName]', function(newVal){
    $scope.fullName = newVal[0] + newVal[1];
}

Awesome! Thanks @matsko, you're awesome.

jonbcard avatar May 10 '13 19:05 jonbcard