Skip to content

Instantly share code, notes, and snippets.

@mitrakmt
Forked from anantn/firebase_first_item.js
Last active September 2, 2015 04:40
Show Gist options
  • Save mitrakmt/729d8a1d18cf0fb4fafe to your computer and use it in GitHub Desktop.
Save mitrakmt/729d8a1d18cf0fb4fafe to your computer and use it in GitHub Desktop.
Firebase: Get the first item in a list. This snippet retrieves only the first item in a list.
function makeList(ref) {
var fruits = ["banana", "apple", "grape", "orange"];
for (var i = 0; i < fruits.length; i++) {
ref.push(fruits[i]);
}
}
function getFirstFromList(ref, cb) {
ref.startAt().limit(1).once("child_added", function(snapshot) {
cb(snapshot.val());
});
}
// Running this should popup an alert with "banana".
function go() {
var testRef = new Firebase("https://example.firebaseIO-demo.com/");
makeList(testRef);
getFirstFromList(testRef, function(val) {
alert(val);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment