@@ -2,27 +2,100 @@ Backfire
2
2
========
3
3
Backfire is an officially supported [ Backbone] ( http://backbonejs.org ) binding for
4
4
[ 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
6
6
automatically synchronize with Firebase, and also allow you to use regular
7
+ ` Backbone.Sync ` based synchronization methods.
7
8
8
9
Live Demo: <a target =" _blank " href =" http://firebase.github.io/backfire/examples/todos/ " >Real-time TODO app</a >.
9
10
10
- Usage
11
- -----
11
+ Getting Started
12
+ ---------------
12
13
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!
13
15
14
- ``` html
16
+ ``` html
15
17
<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 >
17
19
```
18
20
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.
20
23
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
+ ```
22
47
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.
26
99
27
100
``` js
28
101
var TodoList = Backbone .Firebase .Collection .extend ({
@@ -39,50 +112,76 @@ var Messages = Backbone.Firebase.Collection.extend({
39
112
firebase: new Firebase (" https://<your-firebase>.firebaseio.com" ).limit (10 )
40
113
});
41
114
```
42
-
43
115
Any models added to the collection will be synchronized to the provided
44
116
Firebase. Any other clients using the Backbone binding will also receive
45
117
` add ` , ` remove ` and ` changed ` events on the collection as appropriate.
46
118
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
+ ```
51
127
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,
53
129
(via ` add ` and ` remove ` ) and _ remote_ data will be instantly updated.
54
130
Subsequently, the same events will fire on all your other clients immediately.
55
131
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.
58
135
59
- ### METHOD 2: Backbone.sync
136
+ ``` js
137
+ Messages .add ({subject: " Hello" , time: new Date ().getTime ()});
138
+ ```
60
139
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 .
64
143
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
+ ```
69
147
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.
71
153
72
154
``` 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"
76
157
});
77
158
```
78
159
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 ` .
82
161
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
+ ```
85
171
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
+ ```
0 commit comments