Skip to content
This repository has been archived by the owner on Aug 25, 2018. It is now read-only.

Wrap Backbone.Firebase so it can be called in node.js and as an AMD module #71

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@ Getting Started
Include both firebase.js and backbone-firebase.js in your application.
They're both served off of Firebase's CDN, which you are welcome to use!

### As a Script Include
``` html
<script src="https://cdn.firebase.com/v0/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/backfire/0.3.0/backbone-firebase.min.js"></script>
```

Backfire is also available on Bower.
You will now have access to the `Backbone.Firebase`,
`Backbone.Firebase.Collection`, and `Backbone.Firebase.Model` objects.

### Bower Install
``` bash
bower install backfire
```

You will now have access to the `Backbone.Firebase`,
`Backbone.Firebase.Collection`, and `Backbone.Firebase.Model` objects.
### Node Install
``` js
var BackboneFirebase = require('./backbone-firebase.js');
```

Pretty much just like web installs; use `BackboneFirebase.Collection`, and `BackboneFirebase.Model`.

Backbone.Firebase
-----------------
Expand Down
76 changes: 41 additions & 35 deletions backbone-firebase.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
/**
* Backbone Firebase Adapter.
*/

"use strict";

(function() {

var _ = window._;
var Backbone = window.Backbone;

Backbone.Firebase = function(ref) {
(function (win, factory) {
if (typeof module === 'object' && typeof exports === 'object' && exports === module.exports) {
module.exports = factory(require('underscore'), require('backbone'), require('firebase'));
}
else if (typeof define === 'function' && define.amd) {
define(['underscore', 'backbone', 'firebase'], factory);
}
else {
win.Backbone.Firebase = factory(win._, win.Backbone, win.Firebase);
}
}(this, function(_, Backbone, Firebase) {
"use strict";

var BackboneFirebase = function(ref) {
this._fbref = ref;
this._children = [];
if (typeof ref == "string") {
Expand All @@ -22,7 +27,7 @@
this._fbref.on("child_removed", this._childRemoved, this);
};

_.extend(Backbone.Firebase.prototype, {
_.extend(BackboneFirebase.prototype, {
_childAdded: function(childSnap, prevChild) {
var model = childSnap.val();
model.id = childSnap.name();
Expand Down Expand Up @@ -119,7 +124,7 @@
});


Backbone.Firebase.sync = function(method, model, options, error) {
BackboneFirebase.sync = function(method, model, options, error) {
var store = model.firebase || model.collection.firebase;

// Backwards compatibility with Backbone <= 0.3.3
Expand Down Expand Up @@ -160,13 +165,13 @@
Backbone.sync = function(method, model, options, error) {
var syncMethod = Backbone.oldSync;
if (model.firebase || (model.collection && model.collection.firebase)) {
syncMethod = Backbone.Firebase.sync;
syncMethod = BackboneFirebase.sync;
}
return syncMethod.apply(this, [method, model, options, error]);
};

// Custom Firebase Collection.
Backbone.Firebase.Collection = Backbone.Collection.extend({
BackboneFirebase.Collection = Backbone.Collection.extend({
sync: function() {
this._log("Sync called on a Firebase collection, ignoring.");
},
Expand All @@ -183,16 +188,16 @@
this.firebase = options.firebase;
}
switch (typeof this.firebase) {
case "object":
break;
case "string":
this.firebase = new Firebase(this.firebase);
break;
case "function":
this.firebase = this.firebase();
break;
default:
throw new Error("Invalid firebase reference created");
case "object":
break;
case "string":
this.firebase = new Firebase(this.firebase);
break;
case "function":
this.firebase = this.firebase();
break;
default:
throw new Error("Invalid firebase reference created");
}

// Add handlers for remote events.
Expand Down Expand Up @@ -419,7 +424,7 @@
});

// Custom Firebase Model.
Backbone.Firebase.Model = Backbone.Model.extend({
BackboneFirebase.Model = Backbone.Model.extend({
save: function() {
this._log("Save called on a Firebase model, ignoring.");
},
Expand Down Expand Up @@ -449,16 +454,16 @@
this.firebase = options.firebase;
}
switch (typeof this.firebase) {
case "object":
break;
case "string":
this.firebase = new Firebase(this.firebase);
break;
case "function":
this.firebase = this.firebase();
break;
default:
throw new Error("Invalid firebase reference created");
case "object":
break;
case "string":
this.firebase = new Firebase(this.firebase);
break;
case "function":
this.firebase = this.firebase();
break;
default:
throw new Error("Invalid firebase reference created");
}

// Add handlers for remote events.
Expand Down Expand Up @@ -521,4 +526,5 @@

});

})();
return BackboneFirebase;
}));