Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Proposal : Use a custom compare function for objectEquality in $scope.$watch #10069

Open
Toilal opened this issue Nov 15, 2014 · 2 comments · May be fixed by #10096
Open

Proposal : Use a custom compare function for objectEquality in $scope.$watch #10069

Toilal opened this issue Nov 15, 2014 · 2 comments · May be fixed by #10096

Comments

@Toilal
Copy link
Contributor

Toilal commented Nov 15, 2014

It would be useful to be able to pass a function as the 3rd parameter objectEquality of $scope.$watch.

If a function is passed, is will be used instead of angular.equals to check for object equality.

var eq = function(newData, oldData) {
    ...
}

var cb = function() {
    ...
}

$scope.$watch('data',  cb, eq);

It could also provide better performance by implementing specific equality function for watched object.

My concrete use case : I have momentJS objects inside deep data to watch. But angular.equals doesn't work well with momentJS. For equality checking on momentJS object, moment#isSame must be used instead. Using this new functional parameter, i could delegate equality to lodash#isEqual which is able to customize object equality.

@lgalfaso
Copy link
Contributor

This was discussed before #2871, the main issue always came back to one of these points

  • Adding a custom equals forces the need for a custom clone
  • The digest loop is critical for performance, there is a need to be 100% sure that 1/ the custom equals will have no side-effects 2/ it will run fast

That said, I would like to have a PR in place and discuss these point there

@Toilal
Copy link
Contributor Author

Toilal commented Nov 17, 2014

@lgalfaso This the PR here : #10096

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants