Skip to content

Commit

Permalink
Merge pull request #54 from firebase/jw-firebase-2
Browse files Browse the repository at this point in the history
Update Firebase dependency to 2.0.x
  • Loading branch information
jdimond committed Nov 7, 2014
2 parents 97cd30d + 89191cd commit 85b0f89
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 18 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ in its own format and its own location within your Firebase. This allows your ex
and security rules to remain unchanged while still providing you with an easy solution for geo
queries.

#### Example Usage
### Example Usage

Assume you are building an app to rate bars and you store all information for a bar, e.g. name,
business hours and price range, at `/bars/<bar-id>`. Later, you want to add the possibility for
Expand All @@ -33,6 +33,7 @@ location for each bar using GeoFire, using the bar IDs as GeoFire keys. GeoFire
easily query which bar IDs (the keys) are nearby. To display any additional information about the
bars, you can load the information for each bar returned by the query at `/bars/<bar-id>`.


## Live Demos

To see GeoFire in action, you can [play around with our fully-featured demo](https://geofire.firebaseapp.com/sfVehicles/index.html). (Drag the purple circle!)
Expand All @@ -44,9 +45,12 @@ drag around the search radius and see the vehicles update in realtime.
You can find a full list of our demos [here](https://geofire.firebaseapp.com/index.html)
and view the code for each of them in the [examples directory](./examples/) of this repository.
The examples cover some of the common use cases for GeoFire and explain how to protect your data
using security rules.
using Security and Firebase Rules.


## Upgrading GeoFire

## Upgrading from GeoFire 2.x to 3.x
### Upgrading from GeoFire 2.x.x to 3.x.x

GeoFire 3.x has the same API as 2.x but uses a different underlying data structure to store its
location data. If you are currently using 2.x and want to upgrade to 3.x, you must run the
Expand All @@ -60,6 +64,15 @@ $ npm install # install local npm dependencies
$ node migrateToV3.js # display usage instructions
```

### Upgrading from GeoFire 3.0.x to 3.1.x

With the release of GeoFire 3.1.0, this library now uses [the new query functionality found in
Firebase 2.0.0](https://www.firebase.com/blog/2014-11-04-firebase-realtime-queries.html). As a
result, you will need to upgrade to Firebase 2.x.x and add a new `.indexOn` rule to your Security
and Firebase Rules to get the best performance. You can view [the updated rules here](./examples/securityRules/rules.json)
and [read our docs for more information about indexing your data](https://www.firebase.com/docs/security/guide/indexing-data.html).


## Downloading GeoFire

In order to use GeoFire in your project, you need to include the following files in your HTML:
Expand All @@ -69,7 +82,7 @@ In order to use GeoFire in your project, you need to include the following files
<script src="rsvp.min.js"></script>

<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/1.1.0/firebase.js"></script>
<script src="https://cdn.firebase.com/js/client/2.0.2/firebase.js"></script>

<!-- GeoFire -->
<script src="https://cdn.firebase.com/libs/geofire/3.0.3/geofire.min.js"></script>
Expand All @@ -91,10 +104,12 @@ $ npm install geofire --save
$ bower install geofire --save
```


## Getting Started with Firebase

GeoFire requires Firebase in order to store location data. You can [sign up here](https://www.firebase.com/signup/?utm_source=geofire-js) for a free account.


## API Reference

### GeoFire
Expand Down Expand Up @@ -358,6 +373,7 @@ var location2 = [-78.3, 105.6];
var distance = GeoFire.distance(location1, location2); // distance === 12378.536597423461
```


## Promises

GeoFire uses promises when writing and retrieving data. Promises represent the result of a potentially long-running operation and allow code to run asynchronously. Upon completion of the operation, the promise will be "resolved" / "fulfilled" with the operation's result. This result will be passed to the function defined in the promise's `then()` method.
Expand All @@ -372,6 +388,7 @@ promise.then(function(result) {
})
```


## Contributing

If you'd like to contribute to GeoFire, you'll need to run the following commands to get your environment set up:
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"changelog.txt"
],
"dependencies": {
"firebase": "1.1.x",
"firebase": "2.0.x",
"rsvp": "~3.0.13"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
important - Add a new `.indexOn` rule to your Security and Firebase Rules for best performance. See the GitHub documentation for more information.
feature - Upgraded Firebase dependency to 2.0.x.
2 changes: 1 addition & 1 deletion examples/fish1/js/vendor/geofire.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/fish2/js/vendor/geofire.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/fish3/js/vendor/geofire.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/html5Geolocation/js/vendor/geofire.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/queryBuilder/js/vendor/geofire.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion examples/securityRules/rules.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"rules": {
"<your-geo-fire-location>": {
"<your-geofire-node>": {
// Allow anyone to read and write to this node (assuming they respect the schema below)
".read": true,
".write": true,

// Index each location's geohash for faster querying
".indexOn": ["g"],

// Schema validation
"$key": {
".validate": "newData.hasChildren(['g', 'l']) && newData.getPriority().length <= 22 && newData.getPriority().length > 0",
"g": {
Expand Down
2 changes: 1 addition & 1 deletion examples/sfVehicles/js/vendor/geofire.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"package.json"
],
"dependencies": {
"firebase": "1.1.x",
"firebase": "2.0.x",
"rsvp": "~3.0.13"
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/geoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var GeoQuery = function (firebaseRef, queryCriteria) {
* @param {Object} queryState An object storing the current state of the query.
*/
function _cancelGeohashQuery(query, queryState) {
var queryRef = _firebaseRef.startAt(query[0]).endAt(query[1]);
var queryRef = _firebaseRef.orderByChild("g").startAt(query[0]).endAt(query[1]);
queryRef.off("child_added", queryState.childAddedCallback);
queryRef.off("child_removed", queryState.childRemovedCallback);
queryRef.off("child_changed", queryState.childChangedCallback);
Expand Down Expand Up @@ -201,7 +201,7 @@ var GeoQuery = function (firebaseRef, queryCriteria) {
* @param {Firebase DataSnapshot} locationDataSnapshot A snapshot of the data stored for this location.
*/
function _childAddedCallback(locationDataSnapshot) {
_updateLocation(locationDataSnapshot.name(), decodeGeoFireObject(locationDataSnapshot.val()));
_updateLocation(locationDataSnapshot.key(), decodeGeoFireObject(locationDataSnapshot.val()));
}

/**
Expand All @@ -210,7 +210,7 @@ var GeoQuery = function (firebaseRef, queryCriteria) {
* @param {Firebase DataSnapshot} locationDataSnapshot A snapshot of the data stored for this location.
*/
function _childChangedCallback(locationDataSnapshot) {
_updateLocation(locationDataSnapshot.name(), decodeGeoFireObject(locationDataSnapshot.val()));
_updateLocation(locationDataSnapshot.key(), decodeGeoFireObject(locationDataSnapshot.val()));
}

/**
Expand All @@ -219,7 +219,7 @@ var GeoQuery = function (firebaseRef, queryCriteria) {
* @param {Firebase DataSnapshot} locationDataSnapshot A snapshot of the data stored for this location.
*/
function _childRemovedCallback(locationDataSnapshot) {
var key = locationDataSnapshot.name();
var key = locationDataSnapshot.key();
if (_locationsTracked.hasOwnProperty(key)) {
_firebaseRef.child(key).once("value", function(snapshot) {
var location = snapshot.val() === null ? null : decodeGeoFireObject(snapshot.val());
Expand Down Expand Up @@ -299,7 +299,7 @@ var GeoQuery = function (firebaseRef, queryCriteria) {
var query = _stringToQuery(toQueryStr);

// Create the Firebase query
var firebaseQuery = _firebaseRef.startAt(query[0]).endAt(query[1]);
var firebaseQuery = _firebaseRef.orderByChild("g").startAt(query[0]).endAt(query[1]);

// For every new matching geohash, determine if we should fire the "key_entered" event
var childAddedCallback = firebaseQuery.on("child_added", _childAddedCallback);
Expand Down

0 comments on commit 85b0f89

Please sign in to comment.