Created
April 5, 2012 01:02
-
-
Save alexbain/2307019 to your computer and use it in GitHub Desktop.
BackboneFirebase example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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