Skip to content

Commit a6d4f7d

Browse files
committed
Add documentation on Backfire, closes googlearchive#44, googlearchive#26
1 parent 8c2b2ad commit a6d4f7d

File tree

4 files changed

+141
-42
lines changed

4 files changed

+141
-42
lines changed

README.md

Lines changed: 137 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,100 @@ Backfire
22
========
33
Backfire is an officially supported [Backbone](http://backbonejs.org) binding for
44
[Firebase](http://www.firebase.com/?utm_medium=web&utm_source=backfire).
5-
The bindings let you use special Model and Collection types that will
5+
The bindings let you use special model and collection types that will
66
automatically synchronize with Firebase, and also allow you to use regular
7+
`Backbone.Sync` based synchronization methods.
78

89
Live Demo: <a target="_blank" href="http://firebase.github.io/backfire/examples/todos/">Real-time TODO app</a>.
910

10-
Usage
11-
-----
11+
Getting Started
12+
---------------
1213
Include both firebase.js and backbone-firebase.js in your application.
14+
They're both served off of Firebase's CDN, which you are welcome to use!
1315

14-
```html
16+
``` html
1517
<script src="https://cdn.firebase.com/v0/firebase.js"></script>
16-
<script src="backbone-firebase.js"></script>
18+
<script src="https://cdn.firebase.com/libs/backfire/0.3.0/backbone-firebase.min.js"></script>
1719
```
1820

19-
There are two primary ways to use the bindings:
21+
You will now have access to the `Backbone.Firebase`,
22+
`Backbone.Firebase.Collection`, and `Backbone.Firebase.Model` objects.
2023

21-
### METHOD 1: Backbone.Firebase.Collection
24+
Backbone.Firebase
25+
-----------------
26+
The bindings also override `Backbone.sync` to use Firebase. You may consider
27+
this option if you want to maintain an explicit seperation between _local_ and
28+
_remote_ data, and want to use regular Backbone models and collections.
29+
30+
This adapter works very similarly to the
31+
[localStorage adapter](http://documentcloud.github.com/backbone/docs/backbone-localstorage.html)
32+
used in the canonical Todos example.
33+
34+
Please see [todos-sync.js](https://github.com/firebase/backfire/blob/gh-pages/examples/todos/todos-sync.js)
35+
for an example of how to use this feature.
36+
37+
### firebase
38+
You simply provide a `firebase` property in your collection, and that set of
39+
objects will be persisted at that location.
40+
41+
``` js
42+
var TodoList = Backbone.Collection.extend({
43+
model: Todo,
44+
firebase: new Backbone.Firebase("https://<your-namespace>.firebaseio.com")
45+
});
46+
```
2247

23-
You will have access to a new object, `Backbone.Firebase.Collection`. You
24-
may extend this object, and must provide a Firebase URL or a Firebase reference
25-
as the `firebase` property. For example:
48+
You can also do this with a model.
49+
50+
``` js
51+
var MyTodo = Backbone.Model.extend({
52+
firebase: new Backbone.Firebase("https://<your-namespace>.firebaseio.com/myTodo")
53+
});
54+
```
55+
56+
### fetch()
57+
In a collection with the `firebase` property defined, calling `fetch` will
58+
retrieve data from Firebase update the collection with its contents.
59+
60+
``` js
61+
TodoList.fetch();
62+
```
63+
64+
### sync()
65+
In a collection with the `firebase` property defined, calling `sync` will
66+
set the contents of the local collection to the specifeid Firebase location.
67+
68+
``` js
69+
TodoList.sync();
70+
```
71+
72+
### save()
73+
In a model with the `firebase` property defined, calling `save` will set the
74+
contents of the model to the specified Firebase location.
75+
76+
``` js
77+
MyTodo.save();
78+
```
79+
80+
### destroy()
81+
In a model with the `firebase` property defined, calling `destroy` will remove
82+
the contents at the specified Firebase location.
83+
84+
``` js
85+
MyTodo.destroy();
86+
```
87+
88+
Backbone.Firebase.Collection
89+
----------------------------
90+
This is a special collection object that will automatically synchronize its
91+
contents with Firebase. You may extend this object, and must provide a Firebase
92+
URL or a Firebase reference as the `firebase` property.
93+
94+
Each model in the collection will be treated as a `Backbone.Firebase.Model`
95+
(see below).
96+
97+
Please see [todos.js](https://github.com/firebase/backfire/blob/gh-pages/examples/todos/todos.js)
98+
for an example of how to use this special collection object.
2699

27100
```js
28101
var TodoList = Backbone.Firebase.Collection.extend({
@@ -39,50 +112,76 @@ var Messages = Backbone.Firebase.Collection.extend({
39112
firebase: new Firebase("https://<your-firebase>.firebaseio.com").limit(10)
40113
});
41114
```
42-
43115
Any models added to the collection will be synchronized to the provided
44116
Firebase. Any other clients using the Backbone binding will also receive
45117
`add`, `remove` and `changed` events on the collection as appropriate.
46118

47-
**BE AWARE!** The important difference between using a regular collection and
48-
a Firebase collection is that you do not need to call any functions that will
49-
affect _remote_ data. If you call `fetch` or `sync` on the collection, **the
50-
library will ignore it silently**.
119+
**BE AWARE!** You do not need to call any functions that will affect _remote_
120+
data. If you call `fetch` or `sync` on the collection,
121+
**the library will ignore it silently**.
122+
123+
``` js
124+
Messages.fetch(); // DOES NOTHING.
125+
Messages.sync(); // DOES NOTHING.
126+
```
51127

52-
You should add and remove your models to the collection as your normally would,
128+
You should add and remove your models to the collection as you normally would,
53129
(via `add` and `remove`) and _remote_ data will be instantly updated.
54130
Subsequently, the same events will fire on all your other clients immediately.
55131

56-
Please see [todos.js](https://github.com/firebase/backfire/blob/gh-pages/todos.js)
57-
for an example of how to use this special collection object.
132+
### add(model)
133+
Add a new model to the collection. This model will be synchronized to Firebase,
134+
triggering an `add` event both locally and on all other clients.
58135

59-
### METHOD 2: Backbone.sync
136+
``` js
137+
Messages.add({subject: "Hello", time: new Date().getTime()});
138+
```
60139

61-
The bindings also override `Backbone.sync` to use Firebase. You may consider
62-
this option if you want to maintain an explicit seperation between _local_ and
63-
_remote_ data.
140+
### remove(model)
141+
Removes a model from the collection. This model will also be removed from
142+
Firebase, triggering a `remove` event both locally and on all other clients.
64143

65-
This adapter works very similarly to the
66-
[localStorage adapter](http://documentcloud.github.com/backbone/docs/backbone-localstorage.html)
67-
used in the canonical Todos example. You simply provide a `firebase` property
68-
in your Model or Collection, and that object will be persisted at that location.
144+
``` js
145+
Messages.remove(someModel);
146+
```
69147

70-
For example:
148+
Backbone.Firebase.Model
149+
-----------------------
150+
This is a special model object that will automatically synchronize its
151+
contents with Firebase. You may extend this object, and must provide a Firebase
152+
URL or a Firebase reference as the `firebase` property.
71153

72154
```js
73-
var TodoList = Backbone.Collection.extend({
74-
model: Todo,
75-
firebase: new Backbone.Firebase("https://<your-namespace>.firebaseio.com")
155+
var MyTodo = Backbone.Firebase.Model.extend({
156+
firebase: "https://<your-firebase>.firebaseio.com/mytodo"
76157
});
77158
```
78159

79-
will ensure that any calls to `fetch` or `sync` on the collection will update
80-
the provided Firebase with the appropriate data. The same is true for the
81-
`save` and `destroy` methods on a model.
160+
You may apply limits as with `Backbone.Firebase.Collection`.
82161

83-
Please see [todos-sync.js](https://github.com/firebase/backfire/blob/gh-pages/todos-sync.js)
84-
for an example of how to use this feature.
162+
**BE AWARE!** You do not need to call any functions that will affect _remote_
163+
data. If you call `save`, `sync` or `fetch` on the model,
164+
**the library will ignore it silently**.
165+
166+
``` js
167+
MyTodo.save(); // DOES NOTHING.
168+
MyTodo.sync(); // DOES NOTHING.
169+
MyTodo.fetch(); // DOES NOTHING.
170+
```
85171

86-
License
87-
-------
88-
[MIT](http://firebase.mit-license.org).
172+
You should modify your model as you normally would, (via `set` and `destroy`)
173+
and _remote_ data will be instantly updated.
174+
175+
### set(value)
176+
Sets the contents of the model and updates it in Firebase.
177+
178+
``` js
179+
MyTodo.set({foo: "bar"}); // Model is instantly updated in Firebase (and other clients).
180+
```
181+
182+
### destroy()
183+
Removes the model locally, and from Firebase.
184+
185+
``` js
186+
MyTodo.destroy(); // Model is instantly removed from Firebase (and other clients).
187+
```

backbone-firebase.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
(function() {
88

9-
var _ = this._;
10-
var Backbone = this.Backbone;
9+
var _ = window._;
10+
var Backbone = window.Backbone;
1111

1212
Backbone.Firebase = function(ref) {
1313
this._fbref = ref;

0 commit comments

Comments
 (0)