fire-child
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -11,17 +11,12 @@ //Require Firebase dependency | ||
//FireChild on method | ||
FireChild.prototype.on = function (action, levels, callback) { | ||
var root = this.ref; | ||
FireChild.prototype.on = function (depth, eventType, callback, cancelCallback, context) { | ||
var calcDepth = function (path) { | ||
return path.match(/\//g).length - (path.slice(-1) == '/' ? 3 : 2); | ||
}; | ||
//Check levels | ||
if (levels < 1 || levels + calcDepth(this.ref.toString()) > 32) { | ||
throw new Error('Invalid levels value (' + levels + ')'); | ||
} | ||
var initialDepth = calcDepth(this.ref.toString()); | ||
var traverse = function (ref) { | ||
if (calcDepth(ref.toString()) == calcDepth(root.toString()) + levels) { | ||
ref.on(action, callback); | ||
if (calcDepth(ref.toString()) == initialDepth + depth) { | ||
ref.on(eventType, callback, cancelCallback, context); | ||
} else { | ||
@@ -33,4 +28,9 @@ ref.on('child_added', function (childSnapshot) { | ||
}; | ||
//Check depth | ||
if (depth < 1 || depth + initialDepth > 32) { | ||
throw new Error('Invalid depth (' + depth + ')'); | ||
} | ||
root.on('child_added', function (childSnapshot) { | ||
this.ref.on('child_added', function (childSnapshot) { | ||
traverse(childSnapshot.ref()); | ||
@@ -37,0 +37,0 @@ }); |
{ | ||
"name": "fire-child", | ||
"version": "0.1.0", | ||
"description": "attaches listeners to grandchildren of a Firebase reference", | ||
"version": "0.1.1", | ||
"description": "A Node.js module which attaches listeners to grandchildren of a Firebase reference.", | ||
"main": "lib/fire-child.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,4 +0,55 @@ | ||
FireChild | ||
========= | ||
# FireChild | ||
attaches listeners to grandchildren of a Firebase references | ||
[![Build Status](https://travis-ci.org/davidcaseria/FireChild.svg?branch=master)](https://travis-ci.org/davidcaseria/FireChild) | ||
[![NPM version](https://badge.fury.io/js/fire-child.svg)](http://badge.fury.io/js/fire-child) | ||
A Node.js module which attaches listeners to grandchildren of a Firebase reference. | ||
Read more about Firebase listeners here: https://www.firebase.com/docs/web/api/query/on.html. | ||
## Installation | ||
To install FireChild, run the following command: | ||
```bash | ||
$ npm install fire-child | ||
``` | ||
## API Reference | ||
### FireChild | ||
A `FireChild` instance is used to add listeners to grandchild locations. | ||
```JavaScript | ||
var FireChild = require('fire-child'); | ||
``` | ||
#### new FireChild(ref) | ||
Creates and returns a new `FireChild` instance to add listeners. Note that this `ref` can point to anywhere in your Firebase and can either be a string or an instance of the Firebase object. | ||
```JavaScript | ||
// Create a FireChild object | ||
var fireChild = new FireChild('https://<your-firebase>.firebaseio.com/'); | ||
``` | ||
#### FireChild.on(depth, eventType, callback, [cancelCallback], [context]) | ||
Adds a Firebase listener at the specified depth from the reference which created the `FireChild`. | ||
```JavaScript | ||
// Add child_added eventType to grandchildren of Firebase reference | ||
fireChild.on(1, 'child_added', function (childSnapshot, prevChildName) {}); | ||
``` | ||
## Contributing | ||
If you'd like to contribute to FireChild, you'll need to run the following commands to get your environment set up: | ||
```bash | ||
$ git clone https://github.com/davidcaseria/FireChild.git | ||
$ cd FireChild # go to the geofire directory | ||
$ npm install -g mocha # globally install mocha test framework | ||
$ npm install # locally install all dependencies | ||
``` |
@@ -5,31 +5,28 @@ var Firebase = require('firebase'), | ||
var ref = new Firebase('https://fire-child.firebaseio.com/'); | ||
ref.set({ | ||
level1: true, | ||
level2: { | ||
level1: true | ||
}, | ||
level3: { | ||
level2: { | ||
level1: true | ||
} | ||
}, | ||
level4: { | ||
level3: { | ||
describe('FireChild', function () { | ||
before(function (done) { | ||
var ref = new Firebase('https://fire-child.firebaseio.com/'); | ||
ref.set({ | ||
level1: true, | ||
level2: { | ||
level1: true | ||
}, | ||
level3: { | ||
level2: { | ||
level1: true | ||
} | ||
}, | ||
level4: { | ||
level3: { | ||
level2: { | ||
level1: true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}, function () { | ||
done(); | ||
}); | ||
}); | ||
var fireChild = new FireChild('https://fire-child.firebaseio.com/'); | ||
fireChild.on('child_added', 3, function (childSnapshot, prevChildName) { | ||
console.log(childSnapshot.val()); | ||
}); | ||
describe('FireChild', function () { | ||
describe('#constructor()', function () { | ||
@@ -66,8 +63,34 @@ it('should exist', function () { | ||
it('should not accept level 0', function () { | ||
it('should not accept depth 0', function () { | ||
should(function () { | ||
fireChild.on('child_added', 0, function () {}) | ||
fireChild.on(0, 'child_added', function () {}); | ||
}).throw(); | ||
}); | ||
var createDepthTest = function (depth, expectedChildren) { | ||
return function (done) { | ||
var children = 0; | ||
fireChild.on(depth, 'child_added', function (childSnapshot) { | ||
if (childSnapshot.val()) { | ||
children++; | ||
if (children == expectedChildren) { | ||
done(); | ||
} | ||
} | ||
}); | ||
}; | ||
}; | ||
it('should return 3 grandchildern', createDepthTest(1, 3)); | ||
it('should return 2 great-grandchildern', createDepthTest(2, 2)); | ||
it('should return 1 great-great-grandchild', createDepthTest(3, 1)); | ||
it('should not accept depth 33', function () { | ||
should(function () { | ||
fireChild.on(33, 'child_added', function () {}); | ||
}).throw(); | ||
}); | ||
}); | ||
}); |
8219
108
55