Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit f25d4c9

Browse files
committed
docs(pipes): improve FetchJsonPipe discussion
1 parent 1be698c commit f25d4c9

File tree

7 files changed

+57
-68
lines changed

7 files changed

+57
-68
lines changed
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// #docregion
22
import { Pipe, PipeTransform } from '@angular/core';
33
import { Http } from '@angular/http';
4+
import './rxjs-extensions';
45

56
// #docregion pipe-metadata
67
@Pipe({
@@ -9,20 +10,20 @@ import { Http } from '@angular/http';
910
})
1011
// #enddocregion pipe-metadata
1112
export class FetchJsonPipe implements PipeTransform {
12-
private fetchedJson: any = null;
13-
private prevUrl = '';
13+
private cachedData: any = null;
14+
private cachedUrl = '';
1415

15-
constructor(private _http: Http) { }
16+
constructor(private http: Http) { }
1617

1718
transform(url: string): any {
18-
if (url !== this.prevUrl) {
19-
this.prevUrl = url;
20-
this.fetchedJson = null;
21-
this._http.get(url)
19+
if (url !== this.cachedUrl) {
20+
this.cachedData = null;
21+
this.cachedUrl = url;
22+
this.http.get(url)
2223
.map( result => result.json() )
23-
.subscribe( result => this.fetchedJson = result );
24+
.subscribe( result => this.cachedData = result );
2425
}
2526

26-
return this.fetchedJson;
27+
return this.cachedData;
2728
}
2829
}

public/docs/_examples/pipes/ts/app/hero-async-message.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// #docregion
22
import { Component } from '@angular/core';
3-
import { Observable } from 'rxjs/Rx';
3+
import { Observable } from 'rxjs/Observable';
4+
import './rxjs-extensions';
45

56
@Component({
67
selector: 'hero-message',

public/docs/_examples/pipes/ts/app/hero-list.component.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Component } from '@angular/core';
33

44
@Component({
55
selector: 'hero-list',
6-
// #docregion template
76
template: `
87
<h2>Heroes from JSON File</h2>
98
@@ -12,9 +11,7 @@ import { Component } from '@angular/core';
1211
</div>
1312
1413
<p>Heroes as JSON:
15-
{{'heroes.json' | fetch | json}}
16-
</p>
17-
`
18-
// #enddocregion template
14+
{{'heroes.json' | fetch | json}}
15+
</p>`
1916
})
2017
export class HeroListComponent { }
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// #docregion
22
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
3-
import 'rxjs/Rx';
43
import { AppModule } from './app.module';
54

65
platformBrowserDynamic().bootstrapModule(AppModule);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Extensions to RxJS used in this app.
2+
import 'rxjs/add/observable/interval';
3+
4+
import 'rxjs/add/operator/map';
5+
import 'rxjs/add/operator/take';

public/docs/ts/_cache/guide/pipes.jade

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -361,57 +361,50 @@ h3#async-pipe The impure #[i AsyncPipe]
361361
The component doesn't have to subscribe to the async data source,
362362
it doesn't extract the resolved values and expose them for binding,
363363
and the component doesn't have to unsubscribe when it is destroyed
364-
(a potent source of memory leaks).
364+
(a potential source of memory leaks).
365365

366366
### An impure caching pipe
367367

368-
Let's write one more impure pipe, a pipe that makes an HTTP request to the server.
369-
Normally, that's a horrible idea.
370-
It's probably a horrible idea no matter what we do.
371-
We're forging ahead anyway to make a point.
372-
Remember that impure pipes are called every few microseconds.
373-
If we're not careful, this pipe will punish the server with requests.
368+
Let's write one more impure pipe, a pipe that makes an HTTP request.
374369

375-
We are careful. Our pipe only makes a server call if the request URL has changed.
376-
It caches the request URL and waits for a result which it also caches when it arrives.
377-
The pipe returns the cached result (which is null while a request is in flight)
378-
after every Angular call and only contacts the server as necessary.
370+
Remember that impure pipes are called every few milliseconds.
371+
If we're not careful, this pipe will punish the server with requests.
379372

380-
Here's the code, which uses the [Angular http](server-communication.html) facility
381-
to retrieve a `heroes.json` file:
373+
We are careful.
374+
The pipe only calls the server when the request URL changes and it caches the server response.
375+
Here's the code, which uses the [Angular http](server-communication.html) client to retrieve data:
382376

383377
+makeExample('pipes/ts/app/fetch-json.pipe.ts', null, 'app/fetch-json.pipe.ts')
384378
:marked
385-
Then we demonstrate it in a harness component whose template defines two bindings to this pipe.
386-
+makeExample('pipes/ts/app/hero-list.component.ts', 'template', 'app/hero-list.component.ts (template)')
387-
:marked
388-
Despite the two bindings and what we know to be frequent pipe calls,
389-
the nework tab in the browser developer tools confirms that there is only one request for the file.
379+
Then we demonstrate it in a harness component whose template defines two bindings to this pipe,
380+
both requesting the heroes from the `heroes.json` file.
390381

382+
+makeExample('pipes/ts/app/hero-list.component.ts', null, 'app/hero-list.component.ts')
383+
:marked
391384
The component renders like this:
392385

393386
figure.image-display
394387
img(src='/resources/images/devguide/pipes/hero-list.png' alt="Hero List")
395388

389+
:marked
390+
A breakpoint on the pipe's request for data shows that
391+
* each binding gets its own pipe instance
392+
* each pipe instance caches its own url and data
393+
* each pipe instance only calls the server once
394+
396395
:marked
397396
### *JsonPipe*
398397

399-
The second binding involving the `FetchPipe` uses more pipe chaining.
400-
We take the same fetched results displayed in the first binding
401-
and display them again, this time in JSON format by chaining through to the built-in `JsonPipe`.
398+
The second `fetch` pipe binding above demonstrates more pipe chaining.
399+
It displays the same hero data in JSON format by chaining through to the built-in `JsonPipe`.
402400

403401
.callout.is-helpful
404402
header Debugging with the json pipe
405403
:marked
406404
The [JsonPipe](../api/common/index/JsonPipe-pipe.html)
407405
provides an easy way to diagnosis a mysteriously failing data binding or
408406
inspect an object for future binding.
409-
410-
:marked
411-
Here's the complete component implementation:
412-
413-
+makeExample('pipes/ts/app/hero-list.component.ts', null, 'app/hero-list.component.ts')
414-
407+
415408
a(id="pure-pipe-pure-fn")
416409
:marked
417410
### Pure pipes and pure functions

public/docs/ts/latest/guide/pipes.jade

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -365,40 +365,38 @@ h3#async-pipe The impure #[i AsyncPipe]
365365

366366
### An impure caching pipe
367367

368-
Let's write one more impure pipe, a pipe that makes an HTTP request to the server.
369-
Normally, that's a horrible idea.
370-
It's probably a horrible idea no matter what we do.
371-
We're forging ahead anyway to make a point.
372-
Remember that impure pipes are called every few microseconds.
373-
If we're not careful, this pipe will punish the server with requests.
368+
Let's write one more impure pipe, a pipe that makes an HTTP request.
374369

375-
We are careful. Our pipe only makes a server call if the request URL has changed.
376-
It caches the request URL and waits for a result which it also caches when it arrives.
377-
The pipe returns the cached result (which is null while a request is in flight)
378-
after every Angular call and only contacts the server as necessary.
370+
Remember that impure pipes are called every few milliseconds.
371+
If we're not careful, this pipe will punish the server with requests.
379372

380-
Here's the code, which uses the [Angular http](server-communication.html) facility
381-
to retrieve a `heroes.json` file:
373+
We are careful.
374+
The pipe only calls the server when the request URL changes and it caches the server response.
375+
Here's the code, which uses the [Angular http](server-communication.html) client to retrieve data:
382376

383377
+makeExample('pipes/ts/app/fetch-json.pipe.ts', null, 'app/fetch-json.pipe.ts')
384378
:marked
385-
Then we demonstrate it in a harness component whose template defines two bindings to this pipe.
386-
+makeExample('pipes/ts/app/hero-list.component.ts', 'template', 'app/hero-list.component.ts (template)')
387-
:marked
388-
Despite the two bindings and what we know to be frequent pipe calls,
389-
the nework tab in the browser developer tools confirms that there is only one request for the file.
379+
Then we demonstrate it in a harness component whose template defines two bindings to this pipe,
380+
both requesting the heroes from the `heroes.json` file.
390381

382+
+makeExample('pipes/ts/app/hero-list.component.ts', null, 'app/hero-list.component.ts')
383+
:marked
391384
The component renders like this:
392385

393386
figure.image-display
394387
img(src='/resources/images/devguide/pipes/hero-list.png' alt="Hero List")
395388

389+
:marked
390+
A breakpoint on the pipe's request for data shows that
391+
* each binding gets its own pipe instance
392+
* each pipe instance caches its own url and data
393+
* each pipe instance only calls the server once
394+
396395
:marked
397396
### *JsonPipe*
398397

399-
The second binding involving the `FetchPipe` uses more pipe chaining.
400-
We take the same fetched results displayed in the first binding
401-
and display them again, this time in JSON format by chaining through to the built-in `JsonPipe`.
398+
The second `fetch` pipe binding above demonstrates more pipe chaining.
399+
It displays the same hero data in JSON format by chaining through to the built-in `JsonPipe`.
402400

403401
.callout.is-helpful
404402
header Debugging with the json pipe
@@ -407,11 +405,6 @@ figure.image-display
407405
provides an easy way to diagnosis a mysteriously failing data binding or
408406
inspect an object for future binding.
409407

410-
:marked
411-
Here's the complete component implementation:
412-
413-
+makeExample('pipes/ts/app/hero-list.component.ts', null, 'app/hero-list.component.ts')
414-
415408
a(id="pure-pipe-pure-fn")
416409
:marked
417410
### Pure pipes and pure functions

0 commit comments

Comments
 (0)