Skip to content

Instantly share code, notes, and snippets.

@MiroHibler
Last active January 8, 2016 11:37
Show Gist options
  • Save MiroHibler/4742043 to your computer and use it in GitHub Desktop.
Save MiroHibler/4742043 to your computer and use it in GitHub Desktop.
Firebase: Remove the last item in a list. This snippet removes only the last item in a list.
// Run test @ http://jsfiddle.net/NRJ4d/
function makeList(ref) {
var fruits = ["banana", "apple", "grape", "orange"];
for (var i = 0; i < fruits.length; i++) {
ref.push(fruits[i]);
}
}
function getLastFromList(ref, onComplete) {
ref.limit(1).once("child_added", function(s) {
onComplete(s);
});
}
function pop(ref, onComplete) {
// Remove last entry of the list
getLastFromList(ref, function(s) {
if (s.typeOf === "object") {
// jQuery version
// var val = $.extend(true, {}, s);
// Pure javascript version
var val = JSON.parse(JSON.stringify(s));
} else {
var val = s.val();
}
s.ref().remove(onComplete(val));
});
}
// Running this should popup an alert with "orange"
// and "orange" should be removed from the list
function go() {
var testRef = new Firebase("https://example.firebaseIO-demo.com/");
makeList(testRef);
pop(testRef, function(last_item) {
alert(last_item);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment