0

I'm doing some work on existing angularjs code, and I have something of that sort: I have a list of objects on the client side, which I transfer via a put request to a server side.

The existing code simply takes the list of objects in the $scope and puts it in the json as is.

I want to implement a mechanism where only the objects that have been changed since some event would be sent.

I can do that hardcodedly and save another list of objects and clear it when I submit the changes, but I want something cleaner - f.e, a Changes aware list,

Then, I could do something in the sort of list.changedObjects.

I couldn't seem to find anything like that from basic research, so I was hoping you guys would know something about it.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Giora Guttsait
  • 1,279
  • 15
  • 29

2 Answers2

0

I feel this is a use-case of Observer pattern. https://github.com/melanke/Watch.JS has an lib/example to observe changes in javascript object. Then you can trigger an event in Angular to do your job accordingly http://jsfiddle.net/2zT4C/23/

notnotundefined
  • 3,493
  • 3
  • 30
  • 39
0

$watch helps to listen for $scope changes


AngularJS can then check the value returned against the value the watch function returned the last time. That way AngularJS can determine if the value has changed.

$scope.$watch('list', $scope.sendNewList, true);

it will will update innerHtml if new value of object is not equal to old value of object i.e if it has been changed.

For detailed info check this - $watch() or $watch

AngularJS watch array of objects for data change

Community
  • 1
  • 1
Amit Sirohiya
  • 333
  • 1
  • 13