<script src="http://static.firebase.com/v0/firebase.js"></script>
Example for <a href='http://stackoverflow.com/q/24714529/209103'>Notification of final commit in batch transactions</a><br/>
<button id='init'>Create (or re-create) items</button>
<div id='log'></div>
<button id='action'>Check-in/check-out some items</button>
JavaScript
var ref = new Firebase("https://stackoverflow.firebaseio.com/24714529/");
var items = ref.child("items");
var username = prompt("What\'s your name? We'll use it to check out some items in your name.");
var ITEM_COUNT = 10;
var log = document.getElementById('log');
document.getElementById('init').addEventListener('click', function(event) {
for (var i=0; i < ITEM_COUNT; i++) {
items.child(i).set({
name: 'item_' + i,
});
}
});
function getItemAsString(item) {
return item.name + ': ' + (item.checked_out_by ? ' checked_out_by='+item.checked_out_by : '');
}
items.on('child_added', function(snapshot) {
var val = snapshot.val();
var pre = document.createElement('pre');
pre.id = snapshot.val().name;
pre.innerText = getItemAsString(val);
log.appendChild(pre);
});
items.on('child_changed', function(snapshot) {
var val = snapshot.val();
var pre = document.getElementById(val.name);
console.log('value toggle: '+val.name);
pre.innerText = getItemAsString(val);
});
function toggleRandomItem() {
var i = Math.floor(ITEM_COUNT * Math.random());
items.child(i).transaction(function(current_value) {
if (!current_value.checked_out_by) {
// not checked out -> check it out
current_value.checked_out_by = username;
}
else if (current_value.checked_out_by == username) {
// checked out by me -> check it in
delete current_value.checked_out_by;
}
else {
// checked out by someone else -> abort transaction
return;
}
return current_value;
}, function(error, committed, snapshot) {
if (error) {
console.log('error callback: '+error);
throw error;
} else if (!committed) {
console.log('not committed callback');
console.log(snapshot.val()); // this is unmodified, so no need to update
} else {
...
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.
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!
Console
Debug your Fiddle with a minimal built-in JavaScript console.