BackFire is an officially supported Backbone binding for Firebase. The bindings let you use a special Collection type that will automatically synchronize all models contained within it to Firebase, without the need to make explicit calls to save or sync.
Live Demo: Real-time TODO app.
Include both firebase.js and backbone-firebase.js in your application.
<script src="https://cdn.firebase.com/v0/firebase.js"></script>
<script src="backbone-firebase.js"></script>
There are two primary ways to use the bindings:
You will have access to a new object, Backbone.Firebase.Collection
. You
may extend this object, and must provide a Firebase URL or a Firebase reference
as the firebase
property. For example:
var TodoList = Backbone.Firebase.Collection.extend({
model: Todo,
firebase: "https://<your-firebase>.firebaseio.com"
});
You may also apply a limit
or some other query
on a reference and pass it in:
var Messages = Backbone.Firebase.Collection.extend({
firebase: new Firebase("https://<your-firebase>.firebaseio.com").limit(10)
});
Any models added to the collection will be synchronized to the provided
Firebase. Any other clients using the Backbone binding will also receive
add
, remove
and changed
events on the collection as appropriate.
BE AWARE! The important difference between using a regular collection and
a Firebase collection is that you do not need to call any functions that will
affect remote data. If you call fetch
or sync
on the collection, the
library will ignore it silently.
You should add and remove your models to the collection as your normally would,
(via add
and remove
) and remote data will be instantly updated.
Subsequently, the same events will fire on all your other clients immediately.
Please see todos.js for an example of how to use this special collection object.
The bindings also override Backbone.sync
to use Firebase. You may consider
this option if you want to maintain an explicit seperation between local and
remote data.
This adapter works very similarly to the
localStorage adapter
used in the canonical Todos example. You simply provide a firebase
property
in your Model or Collection, and that object will be persisted at that location.
For example:
var TodoList = Backbone.Collection.extend({
model: Todo,
firebase: new Backbone.Firebase("https://<your-namespace>.firebaseio.com")
});
will ensure that any calls to fetch
or sync
on the collection will update
the provided Firebase with the appropriate data. The same is true for the
save
and destroy
methods on a model.
Please see todos-sync.js for an example of how to use this feature.
MIT.