Last active
December 30, 2016 01:31
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
The code for
$FirebaseArray.$$added()
includes checking for the existence ofsnap.key()
before adding to the array. Is it ok to leave that sanity check out? (In the advanced.js around line 30)