toddmotto / ama

Ask me anything!
20 stars 3 forks source link

Do you think its a good idea to batch $watch functions? #36

Closed timothylombrana closed 8 years ago

timothylombrana commented 8 years ago

What I mean is to have several $scope.$watch functions running in specific view or is it better to use $scope.$watchCollection even if the variables are unrelated? So as to have only one watch function in that file.

toddmotto commented 8 years ago

Gimme some context :P

timothylombrana commented 8 years ago

`$scope.$watch('expressionOne', function(newVal){ console.log(newVal); }); $scope.$watch('expressionTwo', function(newVal){ console.log(newVal); }); $scope.$watch('expressionThree', function(newVal){ console.log(newVal); });

//As oppsed to: //All unrelated variables but being all watched under same function, in same JS file. var collection = ['expressionOne', 'expressionTwo', 'expressionThree']; $scope.$watchCollection('collection', function(newCollection, oldCollection, scope) { // some code here for given expression. });`

toddmotto commented 8 years ago

I see, try out $scope.$watchGroup:

https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watchGroup

timothylombrana commented 8 years ago

Id like to test if it be more performant use $watchGroup, instead of having several $watch functions. What do you think?

toddmotto commented 8 years ago

Well, ideally you shouldn't really need $watches at all, they're usually never needed. If you absolutely need them then I'd use group over collection :)

timothylombrana commented 8 years ago

Then thats what I'll do, not need watchers! Thanks again!