Skip to content

Instantly share code, notes, and snippets.

@ctmcquilkin
Forked from anantn/firebase_remove_pushed.js
Last active July 1, 2019 15:10
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 ctmcquilkin/6722983 to your computer and use it in GitHub Desktop.
Save ctmcquilkin/6722983 to your computer and use it in GitHub Desktop.
function pushSomething(ref) {
// Let's push something. push() returns a reference that you can hold onto!
var justPushed = ref.push({test: "push"});
// We return a reference, but you can also return the name of the newly
// created object with .name().
return justPushed;
}
function removeItem(ref) {
// Now we can get back to that item we just pushed via .child().
ref.remove(function(error) {
alert(error ? "Uh oh!" : "Success!");
});
}
function go() {
var testRef = new Firebase("https://example.firebaseIO.com/");
var newRef = pushSomething(testRef);
// Later... should popup an alert that says "null" (No Error).
removeItem(newRef);
}
/* for testing: */
var name = 'me';
var text = 'something';
var nameRef = new Firebase('https://examplefirebase.firebaseio.com/RemoveTest');
nameRef.child('test').set('anothertest'); // writing data as list (adds node directly under root)
nameRef.child('test').push('moretesting'); // using child instead of set (generated unique child name) (adds "moretesting" to uniquely named child node of test child node of root)
nameRef.push({name: name, text: text}); // automates generation of unique child names (adds uniquely named node under root with two child nodes "me" and "something")
nameRef.set({name: name, text: text}); // object (overwrote all child nodes of root, adds "me" and "something")
nameRef.update({name: name, text: text}); // writing multiple children without overwriting other data
$('#remove').on('click', function(){
console.log('button clicked');
event.preventDefault();
nameRef.child('test').remove(); // this removes the parent of test (RemoveTest)
nameRef.child('name').remove(); // this removes "name:'me'" from root (RemoveTest)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment