Skip to content

Instantly share code, notes, and snippets.

@mlynch
Created June 13, 2012 22:38
Show Gist options
  • Save mlynch/2926943 to your computer and use it in GitHub Desktop.
Save mlynch/2926943 to your computer and use it in GitHub Desktop.
Creating a new resource
initialize: function() {
this._fdb = new Firebase('http://gamma.firebase.com/opencountmadison/');
this._resources = this._fdb.child('resources');
this._bindEvents();
this._bindDataEvents();
},
// Bind interface events
_bindEvents: function() {
var self = this;
$('#add-form').submit(function() {
var name = $('#name-new').val();
var tag = $('#tag-new').val();
var max = parseInt($('#max-new').val());
var current = parseInt($('#count-new').val());
// some sanity checking...
self._addResource(tag, name, max, current);
return false;
});
},
// 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