Skip to content

Commit 9d51a21

Browse files
author
jacobawenger
committed
Fixed issue with limit() queries and added test suite
- Fixed issue when binding to Firebase limit() query - Added error checking - Added basic test suite - Added travis integration - Updated README - Added limit query to todoApp demo
1 parent 2ba546e commit 9d51a21

30 files changed

+5480
-4479
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
2-
./bower_components
2+
bower_components
33
firebase.json
4+
tests/coverage

.jshintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"predef": [
3+
"module",
4+
"Firebase"
5+
],
26
"bitwise": true,
37
"curly": true,
48
"eqeqeq": true,

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- '0.10'
4+
install:
5+
- npm install -g bower
6+
- npm install
7+
- bower install
8+
script:
9+
- npm test

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
v0.1.6
2+
-------------
3+
Release Date: 2014-06-29
4+
5+
* Fixed issue when binding to Firebase limit() query
6+
* Added error checking
7+
* Added basic test suite
8+
* Added distribution files to Firebase CDN
9+
110
v0.1.5
211
-------------
312
Release Date: 2014-05-23

README.md

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,95 @@
11
# ReactFire
22

3-
ReactFireMixin is an officially supported [ReactJS](http://facebook.github.io/react/) mixin
4-
for [Firebase](http://www.firebase.com/). Firebase provides your React app with a
5-
persistent, realtime backend to effortlessly keep all of your clients in sync!
3+
[![Build Status](https://travis-ci.org/firebase/reactfire.svg)](https://travis-ci.org/firebase/reactfire)
4+
[![GitHub version](https://badge.fury.io/gh/firebase%2Freactfire.svg)](http://badge.fury.io/gh/firebase%2Freactfire)
65

7-
Read our [blog post](https://firebase.com/blog/2014-05-01-using-firebase-with-react.html) on using Firebase with React and check out our [live Todo app demo](https://reactfiretodoapp.firebaseapp.com/) to get started!
6+
[ReactJS](http://facebook.github.io/react/) is a framework for building large, complex user interfaces. [Firebase](http://www.firebase.com/) complements it perfectly by providing an easy-to-use, realtime data source for populating the `state` of React components. With ReactFire, it only takes a few lines of JavaScript to integrate Firebase into React apps via the `ReactFireMixin`.
87

9-
## Usage
8+
[Read our blog post](https://firebase.com/blog/2014-05-01-using-firebase-with-react.html) on using Firebase with React and [check out our live Todo app demo](https://reactfiretodoapp.firebaseapp.com/) to get started!
109

11-
The ReactFireMixin can be added to your project in three ways:
10+
## Downloading ReactFire
1211

13-
* Manually copy ReactFireMixin.js from GitHub to you local directory.
14-
* Use bower: `bower install ReactFire`
15-
* Use npm: `npm install reactfire`
12+
In order to use the `ReactFireMixin` in your project, you need to include the following files in your HTML:
1613

17-
To use the ReactFireMixin in a React component, add it to the component's mixins property:
14+
```html
15+
<!-- React JS -->
16+
<script src="http://fb.me/react-0.10.0.min.js"></script>
17+
<script src="http://fb.me/JSXTransformer-0.10.0.js"></script>
1818

19-
var ExampleComponent = React.createClass({
20-
mixins: [ReactFireMixin],
21-
...
22-
});
19+
<!-- Firebase -->
20+
<script src="https://cdn.firebase.com/js/client/1.0.17/firebase.js"></script>
2321

24-
## API Reference
22+
<!-- ReactFire -->
23+
<script src="https://cdn.firebase.com/libs/reactfire/0.1.6/reactfire.min.js"></script>
24+
```
2525

26-
###bindAsArray(firebaseRef, bindVar)
26+
Use the URL above to download both the minified and non-minifed versions of ReactFire from the Firebase CDN. You can also download them from the `/dist/` directory of this GitHub repository. [Firebase](https://www.firebase.com/docs/web-quickstart.html) and [React](http://facebook.github.io/react/downloads.html) can be downloaded directly from their respective websites.
2727

28-
Creates a binding between Firebase and the inputted bind variable as an array. The Firebase
29-
reference will be stored in this.firebaseRefs[bindVar].
28+
You can also install ReactFire via npm or Bower and the dependencies will be downloaded automatically:
3029

31-
this.bindAsArray(new Firebase("https://<YOUR_FIREBASE>/"), "items");
30+
```bash
31+
$ npm install reactfire --save
32+
```
3233

33-
###bindAsObject(firebaseRef, bindVar)
34+
```bash
35+
$ bower install reactfire --save
36+
```
3437

35-
Creates a binding between Firebase and the inputted bind variable as an object. The Firebase
36-
reference will be stored in this.firebaseRefs[bindVar].
38+
## Getting Started with Firebase
3739

38-
this.bindAsObject(new Firebase("https://<YOUR_FIREBASE>/"), "items");
40+
ReactFire requires Firebase in order to store data. You can [sign up here](https://www.firebase.com/signup/) for a free account.
3941

40-
###unbind(bindVar)
42+
## Usage
4143

42-
Removes the binding between Firebase and the inputted bind variable. This removes the stored
43-
Firebase reference in this.firebaseRefs[bindVar] and cleans up any event handlers associated
44-
with that Firebase reference.
44+
To use the `ReactFireMixin` in a React component, add it to the component's mixins property:
4545

46-
this.unbind("items");
46+
```javascript
47+
var ExampleComponent = React.createClass({
48+
mixins: [ReactFireMixin],
49+
...
50+
});
51+
```
4752

48-
## Contributing
53+
The following APIs will then be available from the `this` object inside of `ExampleComponent`.
4954

50-
Interested in manually debugging from source or submitting a pull request? Follow the steps
51-
below.
55+
## API Reference
5256

53-
### Install Dependencies
57+
### bindAsArray(firebaseRef, bindVar)
5458

55-
```bash
56-
$ git clone https://github.com/firebase/ReactFire.git # clone this repository
57-
$ npm install # install local NPM build / test dependencies
59+
Creates a binding between Firebase and the inputted bind variable as an array. The Firebase reference will be stored in `this.firebaseRefs[bindVar]`.
60+
61+
```javascript
62+
this.bindAsArray(new Firebase("https://<YOUR_FIREBASE>/"), "items");
63+
```
64+
65+
### bindAsObject(firebaseRef, bindVar)
66+
67+
Creates a binding between Firebase and the inputted bind variable as an object. The Firebase reference will be stored in `this.firebaseRefs[bindVar]`.
68+
69+
```javascript
70+
this.bindAsObject(new Firebase("https://<YOUR_FIREBASE>/"), "items");
5871
```
5972

60-
### Compile
73+
### unbind(bindVar)
74+
75+
Removes the binding between Firebase and the inputted bind variable. This removes the stored Firebase reference in `this.firebaseRefs[bindVar]` and cleans up any event handlers associated with that Firebase reference.
76+
77+
```javascript
78+
this.unbind("items");
79+
```
80+
81+
## Contributing
82+
83+
If you'd like to contribute to ReactFire, you'll need to run the following commands to get your environment set up:
6184

6285
```bash
63-
$ gulp
86+
$ git clone https://github.com/firebase/reactfire.git
87+
$ npm install -g gulp # globally intall gulp task runnger
88+
$ npm install # install local npm build / test dependencies
89+
$ bower install # install local JavaScript dependencies
90+
$ gulp watch # watch for source file changes
6491
```
6592

66-
## License
93+
`gulp watch` will watch for changes in the `/src/` directory and lint, concatenate, and minify the source files when a change occurs. The output files - `reactfire.js` and `reactfire.min.js` - are written to the `/dist/` directory.
6794

68-
[Firebase MIT License](http://firebase.mit-license.org)
95+
You can run the test suite by navigating to `file:///path/to/tests/TestRunner.html` or run the tests via the command line using `gulp test`.

bower.json

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
{
2-
"name": "ReactFire",
3-
"version": "0.1.5",
4-
"homepage": "https://github.com/firebase/ReactFire",
2+
"name": "reactfire",
3+
"description": "Firebase mixin for ReactJS",
4+
"version": "0.1.6",
5+
"main": "./dist/reactfire.min.js",
6+
"homepage": "https://github.com/firebase/reactfire/",
7+
"license": "MIT",
58
"authors": [
69
"jacobawenger <[email protected]>"
710
],
8-
"description": "Firebase mixin for ReactJS",
9-
"main": "js/ReactFireMixin.min.js",
1011
"keywords": [
1112
"react",
12-
"firebase"
13+
"firebase",
14+
"mixin"
1315
],
14-
"license": "MIT",
1516
"ignore": [
1617
"**/.*",
17-
"./CHANGELOG.md",
18-
"./gulpfile.js",
19-
"./package.json",
20-
"examples",
18+
"src",
19+
"build",
20+
"tests",
2121
"node_modules",
22-
"bower_components"
23-
]
22+
"bower_components",
23+
"firebase.json",
24+
"package.json",
25+
"gulpfile.js",
26+
"examples"
27+
],
28+
"dependencies": {
29+
"react": "~0.10.0",
30+
"firebase": "1.0.x"
31+
},
32+
"devDependencies": {
33+
"jasmine": "~2.0.0"
34+
}
2435
}

build/footer

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
return ReactFireMixin;
2+
})();
3+
4+
// Export ReactFireMixin if this is being run in node
5+
if (typeof module !== "undefined" && typeof process !== "undefined") {
6+
module.exports = ReactFireMixin;
7+
}

build/header

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ReactFire is an officially supported ReactJS mixin for Firebase. Firebase
2+
// provides your React app with a persistent, realtime backend to effortlessly
3+
// keep all of your clients in sync!
4+
//
5+
// ReactFire 0.1.6
6+
// https://github.com/firebase/reactfire/
7+
// License: MIT
8+
9+
var ReactFireMixin = (function() {
10+
"use strict";

dist/reactfire.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// ReactFire is an officially supported ReactJS mixin for Firebase. Firebase
2+
// provides your React app with a persistent, realtime backend to effortlessly
3+
// keep all of your clients in sync!
4+
//
5+
// ReactFire 0.1.6
6+
// https://github.com/firebase/reactfire/
7+
// License: MIT
8+
9+
var ReactFireMixin = (function() {
10+
"use strict";
11+
var ReactFireMixin = {
12+
/********************/
13+
/* MIXIN LIFETIME */
14+
/********************/
15+
/* Initializes the Firebase binding refs array */
16+
componentWillMount: function() {
17+
this.firebaseRefs = {};
18+
},
19+
20+
/* Removes any remaining Firebase bindings */
21+
componentWillUnmount: function() {
22+
for (var key in this.firebaseRefs) {
23+
if (this.firebaseRefs.hasOwnProperty(key)) {
24+
this.unbind(key);
25+
}
26+
}
27+
},
28+
29+
30+
/*************/
31+
/* BINDING */
32+
/*************/
33+
/* Creates a binding between Firebase and the inputted bind variable as an array */
34+
bindAsArray: function(firebaseRef, bindVar) {
35+
this._bind(firebaseRef, bindVar, true);
36+
},
37+
38+
/* Creates a binding between Firebase and the inputted bind variable as an object */
39+
bindAsObject: function(firebaseRef, bindVar) {
40+
this._bind(firebaseRef, bindVar, false);
41+
},
42+
43+
/* Creates a binding between Firebase and the inputted bind variable as either an array or object */
44+
_bind: function(firebaseRef, bindVar, bindAsArray) {
45+
this._validateBindVar(bindVar);
46+
47+
var error;
48+
if (typeof firebaseRef.ref === "undefined" || firebaseRef.ref() instanceof Firebase === false) {
49+
error = "firebaseRef must be an instance of Firebase";
50+
}
51+
else if (typeof bindAsArray !== "boolean") {
52+
error = "bindAsArray must be a boolean. Got: " + bindAsArray;
53+
}
54+
55+
if (typeof error !== "undefined") {
56+
throw new Error("ReactFire: " + error);
57+
}
58+
59+
this.firebaseRefs[bindVar] = firebaseRef.ref();
60+
firebaseRef.on("value", function(dataSnapshot) {
61+
var newState = {};
62+
if (bindAsArray) {
63+
newState[bindVar] = this._toArray(dataSnapshot.val());
64+
}
65+
else {
66+
newState[bindVar] = dataSnapshot.val();
67+
}
68+
this.setState(newState);
69+
}.bind(this));
70+
},
71+
72+
/* Removes the binding between Firebase and the inputted bind variable */
73+
unbind: function(bindVar) {
74+
this._validateBindVar(bindVar);
75+
76+
if (typeof this.firebaseRefs[bindVar] === "undefined") {
77+
throw new Error("unexpected value for bindVar. \"" + bindVar + "\" was either never bound or has already been unbound");
78+
}
79+
80+
this.firebaseRefs[bindVar].off("value");
81+
delete this.firebaseRefs[bindVar];
82+
},
83+
84+
85+
/*************/
86+
/* HELPERS */
87+
/*************/
88+
/* Validates the name of the variable which is being bound */
89+
_validateBindVar: function(bindVar) {
90+
var error;
91+
92+
if (typeof bindVar !== "string") {
93+
error = "bindVar must be a string. Got: " + bindVar;
94+
}
95+
else if (bindVar.length === 0) {
96+
error = "bindVar must be a non-empty string. Got: \"\"";
97+
}
98+
else if (bindVar.length > 768) {
99+
// Firebase can only stored child paths up to 768 characters
100+
error = "bindVar is too long to be stored in Firebase. Got: " + bindVar;
101+
}
102+
else if (/[\[\].#$\/\u0000-\u001F\u007F]/.test(bindVar)) {
103+
// Firebase does not allow node keys to contain the following characters
104+
error = "bindVar cannot contain any of the following characters: . # $ ] [ /. Got: " + bindVar;
105+
}
106+
107+
if (typeof error !== "undefined") {
108+
throw new Error("ReactFire: " + error);
109+
}
110+
},
111+
112+
113+
/* Returns true if the inputted object is a JavaScript array */
114+
_isArray: function(obj) {
115+
return (Object.prototype.toString.call(obj) === "[object Array]");
116+
},
117+
118+
/* Converts a Firebase object to a JavaScript array */
119+
_toArray: function(obj) {
120+
var out = [];
121+
if (obj) {
122+
if (this._isArray(obj)) {
123+
out = obj;
124+
}
125+
else if (typeof(obj) === "object") {
126+
for (var key in obj) {
127+
if (obj.hasOwnProperty(key)) {
128+
out.push(obj[key]);
129+
}
130+
}
131+
}
132+
}
133+
return out;
134+
}
135+
};
136+
return ReactFireMixin;
137+
})();
138+
139+
// Export ReactFireMixin if this is being run in node
140+
if (typeof module !== "undefined" && typeof process !== "undefined") {
141+
module.exports = ReactFireMixin;
142+
}

dist/reactfire.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)