Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active June 3, 2016 01:50
Show Gist options
  • Save katowulf/936c596071f098738e0695114323ff08 to your computer and use it in GitHub Desktop.
Save katowulf/936c596071f098738e0695114323ff08 to your computer and use it in GitHub Desktop.
Include old/new values in $firebaseArray::$watch method.
angular.factory('Arr', function($firebaseArray) {
function Arr(ref) {
this.oldVals = {};
return $firebaseArray.call(this, ref);
}
Arr.prototype.$$process = function(event, rec, prevKey) {
$firebaseArray.prototype.$$process.apply(this, arguments);
this.oldVals[rec.$id] = rec;
};
Arr.prototype.$$getOldVal = function(key) {
return this.oldVals.hasOwnProperty(key)? this.oldVals[key] : null;
};
Arr.prototype.$$notify = function(event, key, prevChild) {
var eventData = {event: event, key: key, oldVal: this.$$getOldVal(key), newVal: this.$getRecord(key)};
if( angular.isDefined(prevChild) ) {
eventData.prevChild = prevChild;
}
angular.forEach(this._observers, function(parts) {
parts[0].call(parts[1], eventData);
});
};
return $firebaseArray.$extend(Arr);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment