tomvo tomvo - 3 months ago 28
Javascript Question

Angular $watch array and update from server

I'm deep watching an array called

using the following:

$scope.$watch('route.waypoints', _.debounce(function (prev, cur) {
if(!$scope.initialized) return;
console.log('route waypoints DEEP check');
}, 500), true)

in my
method I
the collection to the server.

What I want to do is upon return from the server I want to update some properties in my waypoints collection again. But when I update using the following:

if(data.linked && data.linked.waypoints.length){
$scope.route.waypoints = data.linked.waypoints;
$scope.route.waypoints = [];

$scope.route.days = data.routes[0].days;

the $watch is called again and the whole thing starts over again.
How can I avoid that? I tried using $scope flags but it seems they are ignored again. it's all a bit of a mess now...


Ok I solved this by throwing away the $watches in general. After reading this excellent article: I dediced to move all to ng-change directives and execute when necessary. The $watches introduced a non-testable magic that was the origin of my problem, it was just not readable anymore.