Skip to content

Instantly share code, notes, and snippets.

@mlynch
Created June 12, 2012 18:40
Show Gist options
  • Save mlynch/2919301 to your computer and use it in GitHub Desktop.
Save mlynch/2919301 to your computer and use it in GitHub Desktop.
resources
initialize: function() {
this._fdb = new Firebase('http://gamma.firebase.com/opencountmadison/');
this._resources = this._fdb.child('resources');
},
// Add a new resource
_addResource: function(tag, name, max, count) {
var self = this, now = new Date();
var resourceData = {
tag: tag,
name: name,
max: max,
count: count,
added: now.getTime(),
last_modified: now.getTime()
};
// Check to see if the resource already exists in firebase, otherwise create it
this._resources.child(tag).transaction(function(currentEntry) {
if(currentEntry === null) {
// Return the data we want Firebase to store
return resourceData;
}
// We'll return undefined if the entry already exists
}, function(success) {
if(!success) {
// Did not create it, it already exists
$(document).trigger('resource.alreadyExists', resourceData);
} else {
// We created it
$(document).trigger('resource.created', resourceData);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment