Skip to content

Instantly share code, notes, and snippets.

@dreadjr
Forked from katowulf/advanced.js
Created September 25, 2015 17:34
Show Gist options
  • Select an option

  • Save dreadjr/72f1c6dbacdbee764f56 to your computer and use it in GitHub Desktop.

Select an option

Save dreadjr/72f1c6dbacdbee764f56 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment