-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackbone-firebase.js
231 lines (191 loc) · 6.94 KB
/
backbone-firebase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Backbone <-> Firebase v0.0.4
//
// Started as a fork of Backpusher.js (https://github.com/pusher/backpusher)
//
// Backbone <-> Firebase (c) 2012 Alex Bain
// Backpusher originally (c) 2011-2012 Pusher
//
// @contributor Kato ([email protected])
//
// This script may be freely distributed under the MIT license.
//
(function(exports, undefined){
"use strict";
var BackboneFirebase = function(collection, options) {
this.collection = collection;
this.subscriptions = []; // used by dispose()
// Extend the defaults with the options provided and set as `this.options`.
this.options = Backbone.$.extend({
urlPrefix: BackboneFirebase.DEFAULT_INSTANCE, // use default unless options provides a prefix
idAttribute: '_firebase_name',
events: BackboneFirebase.defaultEvents
}, options);
if( !collection.url ) {
return urlError('collection');
}
// Optionally pass the urlPrefix in.
this.reference = getFirebaseRef(this.options.urlPrefix, collection.url);
// Optionally specify the idAttribute to use.
this.idAttribute = this.options.idAttribute;
this._bindEvents();
this.initialize(collection, options);
return this;
};
_.extend(BackboneFirebase.prototype, Backbone.Events, {
initialize: function() {},
_bindEvents: function() {
var events = this.options.events;
if (!events) return;
_.each(events, function(f, event) {
var fn = disposableEvent(this, event, f);
this.reference.on(event, fn);
}, this);
},
_add: function(pushed_model) {
var Collection = this.collection;
// Set the model id attribute to be the firebase reference name.
var attr = pushed_model.val();
attr[this.idAttribute] = pushed_model.name();
var model = new Collection.model(attr);
Collection.add(model);
this.trigger('remote_create', model);
return model;
},
/**
* Free all resources and stop listening to all events
*/
dispose: function() {
_.each(this.subscriptions, function(sub) { sub.dispose(); });
this.subscriptions = [];
this.collection = null;
this.options = null;
}
});
// Allows the default URL to be set globally (can still be overridden in options, too)
BackboneFirebase.DEFAULT_INSTANCE = 'http://YOURDB.firebaseio.com';
BackboneFirebase.defaultEvents = {
child_added: function(pushed_model) {
return this._add(pushed_model);
},
child_changed: function(pushed_model) {
// Get existing model using the reference name as the model id.
var model = this.collection.get(pushed_model.name());
if (model) {
model = model.set(pushed_model.val());
this.trigger('remote_update', model);
return model;
} else {
return this._add(pushed_model);
}
},
child_removed: function(pushed_model) {
// Get existing model using the reference name as the model id.
var model = this.collection.get(pushed_model.name());
if (model) {
this.collection.remove(model);
this.trigger('remote_destroy', model);
}
return model;
}
};
// store the rest API for future use (some models can be bound to this instead of Firebase)
Backbone.sync_AJAX = Backbone.sync;
// Original Backbone.sync method from v0.9.2
Backbone.sync = function(method, model, options) {
// Verify Firebase object exists
if (typeof Firebase === undefined) return false;
// Default options, unless specified.
options = _.extend({
urlPrefix: BackboneFirebase.DEFAULT_INSTANCE
}, options);
var path = getPath(model, method, model.id);
if( !path ) {
return urlError('model');
}
// Setup the Firebase Reference
var ref = getFirebaseRef(options.urlPrefix, path, method);
// Map CRUD to Firebase actions
switch (method) {
case 'create':
var pushRef = ref.push(model.toJSON(), function (success) {
if (success && options.success) options.success(pushRef.name());
else if (!success && options.error) options.error();
});
break;
case 'read':
ref.once('value', function (data) {
data = _.toArray(data.val());
if (options.success) options.success(data, "success", {});
});
break;
case 'update':
ref.set(model.toJSON(), function (success) {
if (success && options.success) options.success();
else if (!success && options.error) options.error();
});
break;
case 'delete':
ref.remove(function (success) {
if (success && options.success) options.success();
else if (!success && options.error) options.error();
});
break;
default:
break;
}
return ref;
};
function urlError(source) {
typeof(console) !== 'undefined' && console.error && console.error(new Error(source+'.url must be defined'));
}
function getFirebaseRef(prefix, url) {
if( !(prefix in INSTANCES) ) {
// prevent opening multiple instances of Firebase when using the same prefix
// one instance to rule them all, once instance to find them, one instance to bring them all, and in Backbone.bind() them
INSTANCES[prefix] = new Firebase(prefix);
}
return INSTANCES[prefix].child(url);
}
var INSTANCES = {};
var getPath = function(object, method, id) {
var u;
if ( object ) {
if( _.isFunction(object['url']) ) {
u = object['url']();
}
else {
u = stripSlashes(object['url']);
switch(method) {
case 'update':
case 'read':
case 'delete':
if( id ){
u = u + '/' + id;
}
else {
u = null;
}
break;
case 'create':
break; // leave it alone
default:
// do nothing
}
}
}
return u;
};
function stripSlashes(u) {
var last = u.length-1;
if(u.indexOf('/') === 0) { u = u.substr(1); }
if(u.indexOf('/') === last) { u = u.substr(0, last); }
return u;
}
// only intended to be used for Firebase on/off functions
function disposableEvent(self, event, fn) {
var boundFn = _.bind(fn, self);
self.subscriptions.push({ dispose: function() {self.reference.off(event, boundFn)} });
return boundFn;
}
exports.BackboneFirebase = BackboneFirebase;
})((typeof exports !== 'undefined' ? exports : this));