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

Commit 2e569c2

Browse files
committed
docs(pipes/dart): improve FetchJsonPipe discussion
Followup to #2923 ``` Suites passed: public/docs/_examples/pipes/dart ```
1 parent bc416b8 commit 2e569c2

File tree

4 files changed

+50
-39
lines changed

4 files changed

+50
-39
lines changed

public/docs/_examples/pipes/dart/lib/fetch_json_pipe.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import 'package:angular2/angular2.dart';
88
@Pipe(name: 'fetch', pure: false)
99
// #enddocregion pipe-metadata
1010
class FetchJsonPipe extends PipeTransform {
11-
dynamic _fetchedJson;
12-
String _prevUrl;
11+
dynamic _cachedData;
12+
String _cachedUrl;
1313

1414
dynamic transform(String url) {
15-
if (url != _prevUrl) {
16-
_prevUrl = url;
17-
_fetchedJson = null;
15+
if (url != _cachedUrl) {
16+
_cachedUrl = url;
17+
_cachedData = null;
1818
HttpRequest.getString(url).then((s) {
19-
_fetchedJson = JSON.decode(s);
20-
});
19+
_cachedData = JSON.decode(s);
20+
});
2121
}
22-
return _fetchedJson;
22+
return _cachedData;
2323
}
2424
}

public/docs/_examples/pipes/dart/lib/hero_list_component.dart

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ import 'fetch_json_pipe.dart';
55

66
@Component(
77
selector: 'hero-list',
8-
// #docregion template
98
template: '''
10-
<h4>Heroes from JSON File</h4>
9+
<h2>Heroes from JSON File</h2>
1110
1211
<div *ngFor="let hero of ('heroes.json' | fetch) ">
1312
{{hero['name']}}
1413
</div>
1514
16-
<p>Heroes as JSON:
17-
{{'heroes.json' | fetch | json}}
18-
</p>
15+
<p>Heroes as JSON: {{'heroes.json' | fetch | json}}</p>
1916
''',
2017
pipes: const [FetchJsonPipe])
2118
class HeroListComponent {}

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

+21-14
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,16 @@ figure.image-display
162162
figure.image-display
163163
img(src='/resources/images/devguide/pipes/power-booster.png' alt="Power Booster")
164164

165+
- var _decls = _docsFor == 'dart' ? 'pipes' : 'declarations';
166+
- var _appMod = _docsFor == 'dart' ? '@Component' : 'AppModule';
165167
:marked
166168
Two things to note:
167-
1. We use our custom pipe the same way we use the built-in pipes.
168169

169-
1. We must include our pipe in the `pipes` #{_array} of the `@Component` #{_decorator}.
170+
1. We use our custom pipe the same way we use built-in pipes.
171+
1. We must include our pipe in the `!{_decls}` #{_array} of the `!{_appMod}`.
170172

171173
.callout.is-helpful
172-
header Remember the pipes #{_array}!
174+
header Remember the !{_decls} #{_array}!
173175
:marked
174176
Angular reports an error if we neglect to list our custom pipe.
175177
We didn't list the `DatePipe` in our previous example because all
@@ -186,7 +188,7 @@ figure.image-display
186188
We could upgrade the example to a "Power Boost Calculator" that combines
187189
our pipe and two-way data binding with `ngModel`.
188190

189-
+makeExample('pipes/ts/app/power-boost-calculator.component.ts', null, '/app/power-boost-calculator.component.ts')(format='.')
191+
+makeExample('app/power-boost-calculator.component.ts')
190192

191193
figure.image-display
192194
img(src='/resources/images/devguide/pipes/power-boost-calculator-anim.gif' alt="Power Boost Calculator")
@@ -331,10 +333,16 @@ block pure-change
331333
The only difference is the `pure` flag in the pipe metadata.
332334

333335
This is a good candidate for an impure pipe because the `transform` function is trivial and fast.
334-
+makeExample('pipes/ts/app/flying-heroes.pipe.ts','filter')(format='.')
335336

336-
We can derive a `FlyingHeroesImpureComponent` that we derive from the `FlyingHeroesComponent`.
337-
+makeExample('pipes/ts/app/flying-heroes.component.ts','impure-component','app/flying-heroes.component.ts (FlyingHeroesImpureComponent)')(format='.')
337+
+makeExcerpt('app/flying-heroes.pipe.ts','filter', '')
338+
339+
:marked
340+
We can derive a `FlyingHeroesImpureComponent` from `FlyingHeroesComponent`.
341+
342+
- var _fnSuffix = _docsFor == 'dart' ? '.component.ts' : '-impure.component.html';
343+
- var _region = _docsFor == 'dart' ? 'impure-component' : 'template-flying-heroes';
344+
+makeExcerpt('app/flying-heroes' + _fnSuffix + ' (excerpt)', _region)
345+
338346
:marked
339347
The only substantive change is the pipe in the template.
340348
We can confirm in the <live-example></live-example> that the _flying heroes_
@@ -361,7 +369,7 @@ h3#async-pipe The impure #[i AsyncPipe]
361369
The component doesn't have to subscribe to the async data source,
362370
it doesn't extract the resolved values and expose them for binding,
363371
and the component doesn't have to unsubscribe when it is destroyed
364-
(a potential source of memory leaks).
372+
(a potent source of memory leaks).
365373

366374
### An impure caching pipe
367375

@@ -372,14 +380,14 @@ h3#async-pipe The impure #[i AsyncPipe]
372380

373381
We are careful.
374382
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:
383+
Here's the code<span if-docs="ts">, which uses the [Angular http](server-communication.html) client to retrieve data</span>:
376384

377-
+makeExample('pipes/ts/app/fetch-json.pipe.ts', null, 'app/fetch-json.pipe.ts')
385+
+makeExample('app/fetch-json.pipe.ts')
378386
:marked
379387
Then we demonstrate it in a harness component whose template defines two bindings to this pipe,
380388
both requesting the heroes from the `heroes.json` file.
381389

382-
+makeExample('pipes/ts/app/hero-list.component.ts', null, 'app/hero-list.component.ts')
390+
+makeExample('app/hero-list.component.ts')
383391
:marked
384392
The component renders like this:
385393

@@ -392,7 +400,6 @@ figure.image-display
392400
* each pipe instance caches its own url and data
393401
* each pipe instance only calls the server once
394402

395-
:marked
396403
### *JsonPipe*
397404

398405
The second `fetch` pipe binding above demonstrates more pipe chaining.
@@ -404,8 +411,8 @@ figure.image-display
404411
The [JsonPipe](../api/common/index/JsonPipe-pipe.html)
405412
provides an easy way to diagnosis a mysteriously failing data binding or
406413
inspect an object for future binding.
407-
408-
a(id="pure-pipe-pure-fn")
414+
415+
a#pure-pipe-pure-fn
409416
:marked
410417
### Pure pipes and pure functions
411418

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

+19-12
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,16 @@ figure.image-display
162162
figure.image-display
163163
img(src='/resources/images/devguide/pipes/power-booster.png' alt="Power Booster")
164164

165+
- var _decls = _docsFor == 'dart' ? 'pipes' : 'declarations';
166+
- var _appMod = _docsFor == 'dart' ? '@Component' : 'AppModule';
165167
:marked
166168
Two things to note:
167-
1. We use our custom pipe the same way we use the built-in pipes.
168169

169-
1. We must include our pipe in the `declarations` #{_array} of the AppModule.
170+
1. We use our custom pipe the same way we use built-in pipes.
171+
1. We must include our pipe in the `!{_decls}` #{_array} of the `!{_appMod}`.
170172

171173
.callout.is-helpful
172-
header Remember the declarations #{_array}!
174+
header Remember the !{_decls} #{_array}!
173175
:marked
174176
Angular reports an error if we neglect to list our custom pipe.
175177
We didn't list the `DatePipe` in our previous example because all
@@ -186,7 +188,7 @@ figure.image-display
186188
We could upgrade the example to a "Power Boost Calculator" that combines
187189
our pipe and two-way data binding with `ngModel`.
188190

189-
+makeExample('pipes/ts/app/power-boost-calculator.component.ts', null, '/app/power-boost-calculator.component.ts')(format='.')
191+
+makeExample('app/power-boost-calculator.component.ts')
190192

191193
figure.image-display
192194
img(src='/resources/images/devguide/pipes/power-boost-calculator-anim.gif' alt="Power Boost Calculator")
@@ -331,10 +333,16 @@ block pure-change
331333
The only difference is the `pure` flag in the pipe metadata.
332334

333335
This is a good candidate for an impure pipe because the `transform` function is trivial and fast.
334-
+makeExample('pipes/ts/app/flying-heroes.pipe.ts','filter')(format='.')
335336

336-
We can derive a `FlyingHeroesImpureComponent` that we derive from the `FlyingHeroesComponent`.
337-
+makeExample('pipes/ts/app/flying-heroes-impure.component.html','template-flying-heroes','app/flying-heroes-impure.component.html (FlyingHeroesImpureComponent)')(format='.')
337+
+makeExcerpt('app/flying-heroes.pipe.ts','filter', '')
338+
339+
:marked
340+
We can derive a `FlyingHeroesImpureComponent` from `FlyingHeroesComponent`.
341+
342+
- var _fnSuffix = _docsFor == 'dart' ? '.component.ts' : '-impure.component.html';
343+
- var _region = _docsFor == 'dart' ? 'impure-component' : 'template-flying-heroes';
344+
+makeExcerpt('app/flying-heroes' + _fnSuffix + ' (excerpt)', _region)
345+
338346
:marked
339347
The only substantive change is the pipe in the template.
340348
We can confirm in the <live-example></live-example> that the _flying heroes_
@@ -372,14 +380,14 @@ h3#async-pipe The impure #[i AsyncPipe]
372380

373381
We are careful.
374382
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:
383+
Here's the code<span if-docs="ts">, which uses the [Angular http](server-communication.html) client to retrieve data</span>:
376384

377-
+makeExample('pipes/ts/app/fetch-json.pipe.ts', null, 'app/fetch-json.pipe.ts')
385+
+makeExample('app/fetch-json.pipe.ts')
378386
:marked
379387
Then we demonstrate it in a harness component whose template defines two bindings to this pipe,
380388
both requesting the heroes from the `heroes.json` file.
381389

382-
+makeExample('pipes/ts/app/hero-list.component.ts', null, 'app/hero-list.component.ts')
390+
+makeExample('app/hero-list.component.ts')
383391
:marked
384392
The component renders like this:
385393

@@ -392,7 +400,6 @@ figure.image-display
392400
* each pipe instance caches its own url and data
393401
* each pipe instance only calls the server once
394402

395-
:marked
396403
### *JsonPipe*
397404

398405
The second `fetch` pipe binding above demonstrates more pipe chaining.
@@ -405,7 +412,7 @@ figure.image-display
405412
provides an easy way to diagnosis a mysteriously failing data binding or
406413
inspect an object for future binding.
407414

408-
a(id="pure-pipe-pure-fn")
415+
a#pure-pipe-pure-fn
409416
:marked
410417
### Pure pipes and pure functions
411418

0 commit comments

Comments
 (0)