0

I'm deep watching an array called $scope.route.waypoints using the following:

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

in my saveRoute method I PUT 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;
}else{
        $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...

2 Answers 2

1

You can use different variables vor this data. For example for data which are coming from server you can use

$scope.route.waypoints  

and for your local domain data you can use another domain, for example:

$scope.route.waypointsData

And in that case you will have the more controll because your server data and your local data will be seperated

UPDATE 1

so my suggestion IS

$scope.$watch('route.waypoints',  _.debounce(function (prev, cur) {
  //update data in this place  $scope.route.waypointsData 
}, 500), true)

then also turn watcher on

$scope.$watch('route.waypointsData ',  _.debounce(function (prev, cur) {
  // do your PUT method in that place 
}, 500), true)

and in ng-repeat use waypointsData. You are syncing data between models manually.

Sign up to request clarification or add additional context in comments.

7 Comments

Yeah but I need this one waypoints collection because it is used for displaying in the interface. It's an ng-repeat of all those waypoints on a map as well as a sidebar. I think it will get much more complicated when I do it like you suggest. Also, upon next edit of this waypoint I want the collection to be updated again, and again..
in view you can use your waypointsData , and when $scope.route.waypoints fired you can update your local data
I'm not quite sure if I understand what you mean. I use waypointsData for ng-repeat/ng-model and then $watch(waypoints). But I also want to trigger an update to the server when the user edits the waypoint in the interface. So in that case I should also watch waypointsData which then updates waypoints again. Which results in the same problem right?
no you can update server in waypointsData watcher, and waypoints will be responsible only for updating data from server
sorry man but I don't get it. Can you maybe update your answer to make clear what you mean? How can I watch(wpData) and then only update waypoints on return when that should be represented in the same array?
|
0

Ok I solved this by throwing away the $watches in general. After reading this excellent article: http://www.benlesh.com/2013/10/title.html 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.