Skip to content

Instantly share code, notes, and snippets.

@slashthinking
Created June 20, 2015 09:38
Show Gist options
  • Save slashthinking/19a97f426b976a1ef297 to your computer and use it in GitHub Desktop.
Save slashthinking/19a97f426b976a1ef297 to your computer and use it in GitHub Desktop.
获得列表第一个
// pros: does not fetch entire record set
// cons: grabs the last record which needs to be ignored
var first = true;
var ref = new Firebase(...);
ref.endAt().limit(1).on("child_added", function(snap) {
if( first ) {
first = false;
}
else {
console.log('new record', snap.val());
}
});
// pros: does not return last record
// cons: fetches entire record set
// credits: http://stackoverflow.com/questions/12850789/is-there-a-way-to-know-in-what-context-child-added-is-called-particularly-page/12851236#12851236
var ref = new Firebase(...);
ref.once("value", function(snap) {
//TODO: display initial state...
// Object.keys not supported in IE 8, but has a polyfill: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
var keys = Object.keys(snap.val()||{});
var lastIdInSnapshot = keys[keys.length-1]
ref.startAt(null, lastIdInSnapshot).on("child_added", function(newMessSnapshot) {
//TODO: render new child and flash title bar.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment