Skip to content

Commit c513b08

Browse files
committed
2 parents 14d9dfd + 689e987 commit c513b08

File tree

3 files changed

+92
-100
lines changed

3 files changed

+92
-100
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ Status: Release candidate
2626

2727
```bash
2828
npm install firebase angularfire2 --save
29+
# Or use the 5.0 beta API! Will be released soon
30+
npm install firebase angularfire2@next --save
2931
```
3032

33+
## Note: These docs reference the beta 5.0 API which will be released soon.
34+
3135
## Example use:
3236

3337
```ts

docs/4-querying-lists.md

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
# 4. Querying lists
22

3-
> Querying is a killer feature of AngularFire2.
4-
You can specify query values as observables, and when those
5-
observables emit new values, the query is automatically re-run.
3+
> Querying is a killer feature of AngularFire2.
64
75
## Creating a query with primitive/scalar values
86

9-
Queries are created by specifying a `query` object on the `FirebaseListObservable` options.
7+
Queries are created by building on the [`firebase.database.Reference`](https://firebase.google.com/docs/reference/js/firebase.database.Reference).
108

119
```ts
12-
const queryObservable = db.list('/items', {
13-
query: {
14-
orderByChild: 'size',
15-
equalTo: 'large'
16-
}
17-
});
10+
db.list('/items', ref => ref.orderByChild('size').equalTo('large'))
1811
```
1912

2013
**Query Options:**
@@ -35,70 +28,62 @@ const queryObservable = db.list('/items', {
3528

3629
<sup>2</sup> The Firebase SDK supports an optional `key` parameter for [`startAt`](https://firebase.google.com/docs/reference/js/firebase.database.Reference#startAt), [`endAt`](https://firebase.google.com/docs/reference/js/firebase.database.Reference#endAt), and [`equalTo`](https://firebase.google.com/docs/reference/js/firebase.database.Reference#equalTo) when ordering by child, value, or priority. You can specify the `key` parameter using an object literal that contains the `value` and the `key`. For example: `startAt: { value: 'some-value', key: 'some-key' }`.
3730

31+
To learn more about how sorting and ordering data works in Firebase, check out the Firebase documentation on [working with lists of data](https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data).
32+
3833
## Invalid query combinations
3934

4035
**Queries can only be ordered by one method.** This means you can only specify
4136
`orderByChild`, `orderByKey`, `orderByPriority`, or `orderByValue`.
4237

4338
```ts
4439
// WARNING: Do not copy and paste. This will not work!
45-
const queryObservable = db.list('/items', {
46-
query: {
47-
orderByChild: 'size',
48-
equalTo: 'large',
49-
orderByKey: true,
50-
}
51-
});
40+
ref.orderByChild('size').equalTo('large').orderByKey(true)
5241
```
5342

5443
You can only use `limitToFirst` or `limitToLast`, but not both in combination.
5544

5645
```ts
5746
// WARNING: Do not copy and paste. This will not work!
58-
const queryObservable = db.list('/items', {
59-
query: {
60-
limitToFirst: 10,
61-
limitToLast: 100,
62-
}
63-
});
47+
ref.limitToFirst(10).limitToLast(100)
6448
```
6549

6650
## Creating a query with observable values
6751

68-
Rather than specifying regular values, observables can be used to dynamically
69-
re-run queries when the observable emits a new value.
70-
71-
This is the magic of AngularFire2.
52+
To enable dynamic queries one should lean on RxJS Operators like `switchMap`.
7253

7354
An RxJS Subject is imported below. A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners. See, [What is a Subject](http://reactivex.io/rxjs/manual/overview.html#subject) for more information.
7455

56+
When we call [`switchMap` on the Subject](https://www.learnrxjs.io/operators/transformation/switchmap.html), we cap map each value to a new Observable; in this case a database query.
57+
7558
```ts
76-
const subject = new Subject(); // import {Subject} from 'rxjs/Subject';
77-
const queryObservable = db.list('/items', {
78-
query: {
79-
orderByChild: 'size',
80-
equalTo: subject
81-
}
82-
});
59+
const size$ = new Subject<string>();
60+
const queryObservable = size$.switchMap(size =>
61+
db.list('/items', ref => ref.orderByChild('size').equalTo(size)).valueChanges();
62+
);
8363

8464
// subscribe to changes
8565
queryObservable.subscribe(queriedItems => {
8666
console.log(queriedItems);
8767
});
8868

8969
// trigger the query
90-
subject.next('large');
70+
size$.next('large');
9171

9272
// re-trigger the query!!!
93-
subject.next('small');
73+
size$.next('small');
9474
```
9575

9676
**Example app:**
77+
78+
[See this example in action on StackBlitz](https://stackblitz.com/edit/angularfire-db-api-s8ip7m).
9779

9880
```ts
9981
import { Component } from '@angular/core';
100-
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
101-
import { Subject } from 'rxjs/Subject';
82+
import { AngularFireDatabase, AngularFireAction } from 'angularfire2/database';
83+
import { Observable } from 'rxjs/Observable';
84+
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
85+
import { Subscription } from 'rxjs/Subscription';
86+
import 'rxjs/add/operator/switchMap';
10287

10388
@Component({
10489
selector: 'app-root',
@@ -113,41 +98,49 @@ import { Subject } from 'rxjs/Subject';
11398
<button (click)="filterBy('small')">Small</button>
11499
<button (click)="filterBy('medium')">Medium</button>
115100
<button (click)="filterBy('large')">Large</button>
101+
<button (click)="filterBy(null)" *ngIf="this.size$.getValue()">
102+
<em>clear filter</em>
103+
</button>
116104
</div>
117105
`,
118106
})
119107
export class AppComponent {
120-
items: FirebaseListObservable<any[]>;
121-
sizeSubject: Subject<any>;
108+
items: Observable<AngularFireAction<firebase.database.DataSnapshot>[]>;
109+
size$: BehaviorSubject<string|null>;
122110

123111
constructor(db: AngularFireDatabase) {
124-
this.sizeSubject = new Subject();
125-
this.items = db.list('/items', {
126-
query: {
127-
orderByChild: 'size',
128-
equalTo: this.sizeSubject
129-
}
130-
});
112+
this.size$ = new BehaviorSubject(null);
113+
this.items = this.size$.switchMap(size =>
114+
db.list('/items', ref =>
115+
size ? ref.orderByChild('size').equalTo(size) : ref
116+
).valueChanges();
117+
);
131118
}
132-
filterBy(size: string) {
133-
this.sizeSubject.next(size);
119+
filterBy(size: string|null) {
120+
this.size$.next(size);
134121
}
135122
}
136123
```
137124

138-
+**To run the above example as is, you need to have sample data in you firebase database with the following structure:"**
125+
**To run the above example as is, you need to have sample data in you firebase database with the following structure:**
139126

140-
```ts
141-
-|items
142-
-|item1
143-
-|size: small
144-
-|text: sample small text
145-
-|item2
146-
-|size: medium
147-
-|text: sample medium text
148-
-|item3
149-
-|size: large
150-
-|text: sample large text
127+
```json
128+
{
129+
"items": {
130+
"a" : {
131+
"size" : "small",
132+
"text" : "small thing"
133+
},
134+
"b" : {
135+
"size" : "medium",
136+
"text" : "medium sample"
137+
},
138+
"c" : {
139+
"size" : "large",
140+
"text" : "large widget"
141+
}
142+
}
143+
}
151144
```
152145

153146
### [Next Step: User Authentication](5-user-authentication.md)

docs/Auth-with-Ionic3-Angular4.md

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
# Using AngularFire with Ionic
22

3-
This tutorial provides a walkthrough of integrating ANgularFIre Authentication with Ionic 3 /Angular 4+.
4-
The below setup has been tested on Windows 10, but it should be same for Mac/Linux.
3+
This tutorial provides a walkthrough of integrating AngularFire Authentication with Ionic 3 /Angular 4+.
4+
The below setup has been tested on Windows 10 and macOS Sierra, but it should be same for Linux.
55

66
Note: - If you're working with Ionic2 and Angular2.0, then you should refer to **Auth-with-Ionic2** tutorial
77
[here](https://github.com/angular/angularfire2/blob/master/docs/Auth-with-Ionic2.md)
88

9-
Ensure that you're executing these commands as **Administrator** on Windows and **sudo** on Mac/Linux to avoid any errors.
9+
Ensure that you're executing these commands as **Administrator** on Windows and **sudo** on Mac/Linux if you get any permission errors.
1010

11-
This tutorial uses **Facebook** as the sign-in provider. After completion of this tutorial, you should be able to configure
11+
This tutorial uses **Facebook** as the sign-in provider. After completing this tutorial, you should be able to configure
1212
other sign-in providers like **Twitter**, **Google** on your own.
1313

1414
### Prerequisites
1515
The first step is to ensure you have latest version of **Node** installed.
16-
You can get the latest version from [here](https://nodejs.org).
17-
This will install both node and npm.
16+
You can get the latest version from [nodejs.org](https://nodejs.org).
17+
This will install both `node` and `npm`.
1818

1919
After installing node, check the version by executing the following command in your prompt window:
2020

2121
```bash
2222

23-
C:\projects>node -v
23+
$ node -v
2424
v6.10.2
25-
2625
```
26+
2727
As of this writing, this is the most stable version. If you're not on this version,
28-
please upgrade yourself to latest version by installing node from [here](https://nodejs.org).
28+
please upgrade yourself to latest stable version by installing node from [nodejs.org](https://nodejs.org).
2929

3030
Check your npm version by executing the following command:
3131
```bash
3232

33-
C:\projects>npm -v
33+
$ npm -v
3434
3.10.10
35-
3635
```
3736
Install the Angular CLI, which will be used to build our Angular project and install Angular version 4 or later.
3837

3938
```bash
40-
C:\projects>npm install -g @angular/cli
39+
$ npm install -g @angular/cli
4140
```
4241
Check your angular version by executing the following command:
4342

4443
```bash
45-
C:\projects>ng -v
44+
$ ng -v
4645

4746
@angular/cli: 1.0.0
4847
node: 6.10.2
@@ -57,39 +56,34 @@ Execute the following commands.
5756

5857
```bash
5958

60-
C:\projects>npm install -g cordova
61-
62-
C:\projects>npm install -g ionic
63-
59+
$ npm install -g cordova ionic
6460
```
6561

66-
Once the above commands are executed successfully, check the versions of cordova and ionic by executing the following commands.
62+
Once the above command is executed successfully, check the versions of cordova and ionic:
6763

6864
```bash
69-
C:\projects>cordova -v
65+
$ cordova -v
7066
7.0.1
7167

72-
C:\projects>ionic -v
68+
$ ionic -v
7369
3.4.0
7470
```
7571

76-
These are the latest versions as of this writing.
77-
78-
On successful execution of the above commands, you're all set to create your app with Ionic framework.
72+
You're all set to create your app with Ionic framework.
7973

8074
To create your app, change into the directory where you want your app to reside and execute the following command:
8175

8276
```bash
83-
C:\projects> ionic start auth-ng4-ionic3-af2 blank
77+
$ ionic start auth-ng4-ionic3-af2 blank
8478
```
8579

86-
>The command ionic start will create the project with name "Ionic_AngularFire2_Project" using "blank" template.
80+
>The command ionic start will create the project with name "auth-ng4-ionic3-af2" using "blank" template.
8781
8882
Change to the project directory, which was just created with the above command.
8983

90-
> C:\projects\auth-ng4-ionic3-af2>ionic info
84+
```
85+
$ ionic info
9186
92-
```bash
9387
global packages:
9488
9589
@ionic/cli-utils : 1.4.0
@@ -151,40 +145,41 @@ To start your app, execute the following command:
151145

152146
```bash
153147

154-
C:\projects\auth-ng4-ionic3-af2> ionic serve
155-
148+
$ ionic serve
156149
```
157150
If everything is installed correctly, the app should open your browser with the dev server running at the following url
158151
**`http://localhost:8100`** and will display default home page.
159152

160153
### Configuring AngularFire2 and Firebase
161154

162-
Install angularfire2 and firebase by executing the following command in your project directory:
155+
Install the required packages in your project directory:
163156

164157
```ts
165158

166-
C:\projects\auth-ng4-ionic3-af2>npm install angularfire2 firebase promise-polyfill --save
159+
$ npm install angularfire2 firebase promise-polyfill --save
167160

168161
```
169162

170-
_This should add angularfire2 and firebase entry in your project's package.json file in dependencies section. Something similar_
163+
_This should add angularfire2, promise-polyfill and firebase entry in your project's package.json file as dependencies. Something similar to:_
171164

172-
> "angularfire2": "^4.0.0-rc.1",
173-
174-
> "firebase": "^4.1.3",
165+
```
166+
"angularfire2": "^4.0.0-rc.1",
167+
"firebase": "^4.1.3",
168+
"promise-polyfill": "^6.0.2",
169+
```
175170

176171
### Setup @NgModule
177172

178173
Open your project in your favourite editor and open the `app.module.ts` file, under `src/app`
179174
and add the following three entries.
180175

181-
>1) Import AngularFireModule and AngularFireDatabaseModule at top.
176+
1) Import AngularFireModule and AngularFireDatabaseModule at top.
182177

183-
>2) Define your firebaseConfig constant.
178+
2) Define your firebaseConfig constant.
184179

185-
>3) Initialize your app, by adding AngularFireModule and AngularFireAuthModule in the "imports" array in @NgModule
180+
3) Initialize your app, by adding AngularFireModule and AngularFireAuthModule in the "imports" array in @NgModule
186181

187-
>4) Also, add AngularFireDatabase in the "providers" array in @NgModule
182+
4) Also, add AngularFireDatabase in the "providers" array in @NgModule
188183

189184
your `app.module.ts` file should look something like this.
190185

0 commit comments

Comments
 (0)