Collection attribute watches: $watch('object@attribute') #1093
Description
Often it is useful to be notified if
Given model:
$scope.users = [
{ id: 1, name: 'foo', age: 33},
{ id: 2, name: 'bar', age: 22},
{ id: 3, name: 'baz', age: 11}];
and watch:
$scope.$watch('users@age', function(newVal, oldVal) {
});
the watch would keep track of age
property of each item in the users
collection and it would fire if a value of this property on any of the user object changes or when a user object is added or removed added to/from the collection.
the newVal and oldVal would be an array of age
values with indexes matching the current order of items in the users array.
so during the first digest the watch would fire with these values:
newVal = [33, 22, 11];
oldVal = NaN;
if a new user with age 44 is appended to the array the watch would then fire with:
newVal = [33, 22, 11, 44]
oldVal = [33, 22, 11];
if the only user with age 44 is then updated to 55 the watch would fire with:
newVal = [33, 22, 11, 55];
oldVal = [33, 22, 11, 44];
if the users array is sorted and items in the array are moved around but user is added/removed/deactivated then no watch fires.
if the first user is then updated to age 99 (after sorting) and this user was previously the last user in the array then the watch should fire with:
newVal = [99, 33, 22, 11];
oldVal = [55, 33, 22, 11];
notice that the oldVal contents were reordered, to match the new order of elements in the users
array as well as newVal
array. this is necessary for enabling developers to figure out the actual change (newVal[0] vs oldVal[0]).
implementation notes: it should be possible to reuse hashKeys just like what repeater does to implement this watcher.