Skip to content

Instantly share code, notes, and snippets.

@tivac
Created December 4, 2015 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tivac/a0b596920ea80f2758b5 to your computer and use it in GitHub Desktop.
Save tivac/a0b596920ea80f2758b5 to your computer and use it in GitHub Desktop.
Setting nested objects that maintain order into Firebase
var traverse = require("traverse");
// Traverse object, calculate priority at each level
// so order is maintained (barf barf barf)
traverse(obj).forEach(function(value) {
if(this.isRoot) {
priority = 0;
return;
}
db.child(this.path.join("/")).setWithPriority(
this.isLeaf ? value : "placeholder",
priority
);
if(this.notLeaf) {
priority = 0;
} else {
priority++;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment