<script src="http://cdn.firebase.com/v0/firebase.js"></script>
<ul id="records"></ul>
<form><input type="text"><button>add</button></form>
<li id="recordTemplate">
<span></span> <a href="#">X</a>
<ul></ul>
<form><input type="text"><button>add</button></form>
</li>
<!-- after parsing the records, they look like this
(the hash is stored in data-id):
<li data-id="-iZjYl....">
<span>Title here</span> <a href="#">X</a>
<ul>... children here... </ul>
<form><input type="text"><button>add</button></form>
</li>
-->
var rootRef = new Firebase("https://kato-rp2.firebaseio-demo.com");
// delete record when a tag is clicked
jQuery('body').on('click', 'a', function() {
var $rec = $(this).closest('[data-id]');
var id = $rec.attr('data-id') || null;
if( id ) {
// remove any nested children
$rec.find('[data-id]').each(function() {
rootRef.child($(this).attr('data-id')).remove();
});
// remove the record
rootRef.child(id).remove();
}
return false;
});
// add new records at the appropriate level when a button is clicked
jQuery('body').on('click', 'button', function () {
var $input = $(this).prev('input');
var title = $input.val();
var parent = $input.closest('[data-id]').attr('data-id') || null;
console.log('creating', parent, title);
if (title) {
rootRef.push({ 'title': title, 'parent': parent });
}
$input.val('');
return false;
})
rootRef.on('child_added', function (snapshot) {
var message = snapshot.val();
console.log('child_added', message);
displayTitleMessage(snapshot.name(), message.title, message.parent);
});
rootRef.on('child_removed', function(snapshot) {
$('#records').find('[data-id="'+snapshot.name()+'"]').remove();
});
function displayTitleMessage(id, title, parentId) {
var $parent = parentId ? findParent(parentId) : $('#records');
var $el = makeListItem(title);
console.log('displaying', id, parentId, $parent, title);
// add a data-parent attribute, which we use to locate parent elements
$el.appendTo($parent).attr('data-id', id);
}
function findParent(parentId) {
return $('#records').find('[data-id="' + parentId + '"] > ul');
}
function makeListItem(title) {
return $('#recordTemplate').clone()
// remove the id attr
.attr('id', null)
// enter the <span> tag and use .text() to escape title
.find('span').text(title)
// navigate back to the cloned element and return it
.end();
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.