Skip to content

Instantly share code, notes, and snippets.

@alexbain
Created April 5, 2012 01:02
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 alexbain/2307019 to your computer and use it in GitHub Desktop.
Save alexbain/2307019 to your computer and use it in GitHub Desktop.
BackboneFirebase example
// Create a new instance of the Rooms collections
FireDrop.Rooms = new FireDrop.collections.Rooms();
// Fetch the initial data from Firebase
FireDrop.Rooms.fetch({ success: function (response) {
// Keep this collection in sync with Firebase.
// You could save this as a property on the collection for later access.
new BackboneFirebase(FireDrop.Rooms);
// Create a view for each model and append it to the list.
FireDrop.Rooms.each(function(room) {
// Only create the view if the model doesn't already have one.
if (!room.listItemView) {
new FireDrop.views.roomListItem({
model: room
});
}
// Not included in this gist but it's just a jQuery object.
$roomList.append(room.listItemView.render().el);
});
}});
// If a model is added to the list remotely create a view for it
FireDrop.Rooms.bind('add', function(room) {
if (!room.listItemView) {
new FireDrop.views.roomListItem({
model: room
});
}
$roomList.append(room.listItemView.render().el);
});
// If a model is removed from the list remotely remove the view
FireDrop.Rooms.bind('remove', function(room) {
if (room.listItemView) room.listItemView.remove();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment