Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active December 30, 2016 01:31
Show Gist options
  • Save katowulf/485e9011fc80b61c4e9f to your computer and use it in GitHub Desktop.
Save katowulf/485e9011fc80b61c4e9f to your computer and use it in GitHub Desktop.
Override $FirebaseArray.prototype.$$added in AngularFire (compatible with 0.9.x and above)
// create a class that we will use in place of the pojo (plain old javascript object)
// that is normally created in $FirebaseArray
function Person(snap) {
this.$id = snap.name();
this.updated(snap);
}
Person.prototype = {
updated: function(snap) {
this.data = snap.val();
this.$priority = snap.getPriority();
// can return false here if no updates occur
// which will prevent $watch listeners from being notified
return true;
},
getFullName: function() {
return this.first_name + ' ' + this.last_name;
},
toJSON: function() {
return this.data;
}
}
// now override $FirebaseArray to return our Person class
return $FirebaseArray.$extendFactory({
$$added: function(snap, prevChild) {
// parse data and create record
return new Person(snap);
},
$$updated: function(snap) {
var rec = this.$getRecord(snap.name());
if( rec !== null ) {
return rec.updated(snap);
}
else {
return false;
}
}
});
return $FirebaseArray.$extendFactory({
$$added: function(snap, prevChild) {
var rec = $FirebaseArray.prototype.$$added.call(this, snap);
rec.getFullName = function() { return this.first_name = ' ' + this.last_name; };
return rec;
}
}
@nnyman
Copy link

nnyman commented Dec 2, 2014

The code for $FirebaseArray.$$added() includes checking for the existence of snap.key() before adding to the array. Is it ok to leave that sanity check out? (In the advanced.js around line 30)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment