diff --git a/public/docs/_examples/pipes/ts/.gitignore b/public/docs/_examples/pipes/ts/.gitignore
new file mode 100644
index 0000000000..6724ce3596
--- /dev/null
+++ b/public/docs/_examples/pipes/ts/.gitignore
@@ -0,0 +1 @@
+src/**/*.js
\ No newline at end of file
diff --git a/public/docs/_examples/pipes/ts/package.json b/public/docs/_examples/pipes/ts/package.json
new file mode 100644
index 0000000000..7ba2b6bc56
--- /dev/null
+++ b/public/docs/_examples/pipes/ts/package.json
@@ -0,0 +1,21 @@
+{
+ "name": "angular2-pipes",
+ "version": "1.0.0",
+ "description": "",
+ "main": "index.js",
+ "scripts": {
+ "tsc": "tsc -p src -w",
+ "start": "live-server --open=src"
+ },
+ "keywords": [],
+ "author": "",
+ "license": "ISC",
+ "dependencies": {
+ "angular2": "2.0.0-alpha.44",
+ "systemjs": "0.19.2"
+ },
+ "devDependencies": {
+ "live-server": "^0.8.1",
+ "typescript": "^1.6.2"
+ }
+}
diff --git a/public/docs/_examples/pipes/ts/src/app/app.html b/public/docs/_examples/pipes/ts/src/app/app.html
new file mode 100644
index 0000000000..bcaf614709
--- /dev/null
+++ b/public/docs/_examples/pipes/ts/src/app/app.html
@@ -0,0 +1,46 @@
+
+
The hero's birthday is {{ birthday | date }}
+ + + +The hero's birthday is {{ birthday | date:"MM/dd/yy" }} /p> + + +
+ The chained hero's birthday is + {{ birthday | date | uppercase}} +
+ + + ++ The chained hero's birthday is + {{ ( birthday | date:'fullDate' ) | uppercase}} +
+ + +The hero's birthday is {{ birthday | date }}
` + // #enddocregion hero-birthday-template +}) +export class HeroBirthday { + birthday = new Date(1988,3,15); // April 15, 1988 +} + +bootstrap(HeroBirthday); +// #enddocregion hero-birthday + +// #docregion async-message +@Component({ + selector: 'my-hero', + template: 'Message: {{delayedMessage | async}}', +}) +class MyHeroAsyncMessageComponent { + delayedMessage:PromiseThe hero's birthday is {{ birthday | date:format }}
+ + ` +}) +export class HeroBirthday { + birthday = new Date(1988,3,15); // April 15, 1988 + get format() { return this.toggle ? 'shortDate' : 'fullDate'} + toggle = true; + toggleFormat() { this.toggle = !this.toggle; } +} +// #enddocregion hero-birthday2 diff --git a/public/docs/_examples/pipes/ts/src/app/hero-list-component.ts b/public/docs/_examples/pipes/ts/src/app/hero-list-component.ts new file mode 100644 index 0000000000..7d4e0974e7 --- /dev/null +++ b/public/docs/_examples/pipes/ts/src/app/hero-list-component.ts @@ -0,0 +1,25 @@ +// #docregion +import {bootstrap, Component, + CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/angular2' + +import {FetchJsonPipe} from './fetch-json-pipe' + +@Component({ + selector: 'hero-list', + template: ` +Heroes as JSON: + {{'heroes.json' | fetch | json}} +
+ `, + directives:[CORE_DIRECTIVES], + pipes: [FetchJsonPipe] +}) +export class HeroListComponent { + /* I've got nothing to do ;-) */ +} diff --git a/public/docs/_examples/pipes/ts/src/app/power-boost-calculator.ts b/public/docs/_examples/pipes/ts/src/app/power-boost-calculator.ts new file mode 100644 index 0000000000..7ffad97c7e --- /dev/null +++ b/public/docs/_examples/pipes/ts/src/app/power-boost-calculator.ts @@ -0,0 +1,21 @@ +// #docregion +import {Component, FORM_DIRECTIVES} from 'angular2/angular2' +import {ExponentialStrengthPipe} from './exponential-strength-pipe' + +@Component({ + selector: 'power-boost-calculator', + template: ` ++ Super Hero Power: {{power | exponentialStrength: factor}} +
+ `, + pipes: [ExponentialStrengthPipe], + directives: [FORM_DIRECTIVES] +}) +export class PowerBoostCalculator { + power = 5; + factor = 1; +} diff --git a/public/docs/_examples/pipes/ts/src/app/power-booster.ts b/public/docs/_examples/pipes/ts/src/app/power-booster.ts new file mode 100644 index 0000000000..071dbb0a75 --- /dev/null +++ b/public/docs/_examples/pipes/ts/src/app/power-booster.ts @@ -0,0 +1,15 @@ +// #docregion +import {Component} from 'angular2/angular2' +import {ExponentialStrengthPipe} from './exponential-strength-pipe' + +@Component({ + selector: 'power-booster', + template: ` ++ Super power boost: {{2 | exponentialStrength: 10}} +
+ `, + pipes: [ExponentialStrengthPipe] +}) +export class PowerBooster { } diff --git a/public/docs/_examples/pipes/ts/src/app/window.extension.d.ts b/public/docs/_examples/pipes/ts/src/app/window.extension.d.ts new file mode 100644 index 0000000000..c9ffbfb25a --- /dev/null +++ b/public/docs/_examples/pipes/ts/src/app/window.extension.d.ts @@ -0,0 +1,3 @@ +interface Window { + fetch:(url: string, options?: {}) => PromiseThe hero's birthday is {{ birthday | date }}
` - }) - class HeroBirthday { - birthday = new Date(1988,3,15); // April 15, 1988 - } - - bootstrap(HeroBirthday); ++makeExample('pipes/ts/src/app/app.ts', 'hero-birthday') + :markdown Focus on the component's template to see how we applied the built-in `DatePipe` while binding the `birthday` property. -code-example(format="linenums" escape="html"). -The hero's birthday is {{ birthday | date }}
++makeExample('pipes/ts/src/app/app.html', 'hero-birthday-template')(format=".") :markdown Angular [template syntax](./template-syntax.html#pipe) includes a pipe operator ( | ) which we're @@ -75,8 +65,7 @@ code-example(format="linenums" escape="html"). We'll modify our birthday example to give the date pipe a format parameter. The formatted date should display as **04/15/88**. -code-example(format="linenums" escape="html"). -The hero's birthday is {{ birthday | date:"MM/dd/yy" }}
++makeExample('pipes/ts/src/app/app.html', 'format-birthday')(format=".") :markdown The parameter value can be any valid @@ -85,25 +74,7 @@ code-example(format="linenums" escape="html"). Let's revise our example to bind the pipe's format parameter to the component's `format` property. -code-example(format="linenums" ). - @Component({ - selector: 'hero-birthday', - template: ` - <p>The hero's birthday is {{ birthday | date:format }}</p> - <button (click)="toggleFormat()">Toggle Format</button> - ` - }) - class HeroBirthday { - birthday = new Date(1988,3,15); // April 15, 1988 - format = 'shortDate'; - nextFormat = 'fullDate'; - - toggleFormat() { - let next = this.format; - this.format = this.nextFormat; - this.nextFormat = next; - } - } ++makeExample('pipes/ts/src/app/hero-birthday.2.ts', 'hero-birthday2') :markdown We also added a button to the template and bound its click event to the component's `toggleFormat` method. @@ -111,8 +82,12 @@ code-example(format="linenums" ). ('shortDate') and a longer form ('fullDate'). As we click the button, the displayed date alternates between - "**04/15/88**" and + "**04/15/1988**" and "**Friday, April 15, 1988**". + +figure.image-display + img(src='/resources/images/devguide/pipes/date-format-toggle-anim.gif' alt="Date Format Toggle") +:markdown .l-sub-section :markdown Learn more about the `DatePipes` format options in the [API Docs](../api/core/DatePipe-class.html). @@ -122,23 +97,16 @@ code-example(format="linenums" ). In the following example, we chain the birthday to the `DatePipe` and on to the `UpperCasePipe` so we can display the birthday in uppercase. The following birthday displays as **APR 15, 1988** - -code-example(format="linenums" escape="html"). -- The chained hero's birthday is - {{ birthday | date | uppercase}} -
+ ++makeExample('pipes/ts/src/app/app.html', 'chained-birthday') + :markdown If we pass a parameter to a filter, we have to add parentheses to help the template compiler with the evaluation order. The following example displays **FRIDAY, APRIL 15, 1988** - -code-example(format="linenums" escape="html"). -- The chained hero's birthday is - {{ ( birthday | date:'fullDate' ) | uppercase}} -
+ ++makeExample('pipes/ts/src/app/app.html', 'chained-parameter-birthday') .l-sub-section p Future improvements in the template compiler may eliminate the need for parentheses. @@ -147,35 +115,17 @@ code-example(format="linenums" escape="html"). :markdown ## Custom Pipes - We can easily create our own pipes. + We can write our own custom pipes. - Let's create a custom pipe named `ExponentialStrengthPipe` + Let's make a custom pipe named `ExponentialStrengthPipe` that can boost a hero's powers. + + Create a new file, `exponential-strength-pipe.ts`, and enter the following: -code-example(format="linenums" escape="html"). - import {bootstrap, Component, Pipe} from 'angular2/angular2' - - /* - * Raise the value exponentially - * Takes an exponent argument that defaults to 1. - * Usage: - * value | exponentialStrength:exponent - * Example: - * {{ 2 | exponentialStrength:10}} - * formats to: 1024 - */ - @Pipe({ - name: 'exponentialStrength' - }) - class ExponentialStrengthPipe { - - transform(value:number, args:string[]) : any { - return Math.pow(value, parseInt(args[0] || 1, 10)); - } - } + --> ++makeExample('pipes/ts/src/app/exponential-strength-pipe.ts') :markdown This pipe definition reveals several few key points @@ -191,21 +141,10 @@ code-example(format="linenums" escape="html"). * There will be one item in the array for each parameter passed to the pipe * `transform` returns a modified value that Angular converts to a string. - Now let's create a component to demonstrate our pipe and bootstrap it. - -code-example(format="linenums" escape="html"). - @Component({ - selector: 'power-booster', - template: ` -- Super power boost: {{2 | exponentialStrength: 10}} -
- `, - pipes: [ExponentialStrengthPipe] - }) - class PowerBooster { } - - bootstrap(PowerBooster); + Now let's create a component to demonstrate our pipe. ++makeExample('pipes/ts/src/app/power-booster.ts') +figure.image-display + img(src='/resources/images/devguide/pipes/power-booster.png' alt="Power Booster") :markdown Two things to note: @@ -230,28 +169,11 @@ code-example(format="linenums" escape="html"). We could upgrade the example to a "Power Boost Calculator" that combines our pipe and two-way data binding with `ng-model`. -code-example(format="linenums" ). - import {bootstrap, Component, FORM_DIRECTIVES, Pipe} from 'angular2/angular2' - - @Component({ - selector: 'power-boost-calculator', - template: ` - <h2>Power Boost Calculator</h2> - <div>Normal power: <input [(ng-model)]="power"></div> - <div>Boost factor: <input [(ng-model)]="factor"></div> - <p> - Super Hero Power: {{power | exponentialStrength: factor}} - </p> - `, - pipes: [ExponentialStrengthPipe], - directives: [FORM_DIRECTIVES] - }) - class PowerBoosterCalculator { - power = 5; - factor = 1; - } ++makeExample('pipes/ts/src/app/power-boost-calculator.ts') - bootstrap(PowerBoosterCalculator); +figure.image-display + img(src='/resources/images/devguide/pipes/power-boost-calculator.png' alt="Power Boost Calculator") +:markdown .l-main-section :markdown @@ -276,20 +198,7 @@ code-example(format="linenums" ). It is stateful because the pipe maintains a subscription to the input and its returned values depend on that subscription. In the next example, we bind a simple promise to a view with the async pipe. - -code-example(format="linenums"). - @Component({ - selector: 'my-hero', - template: Message: '{{delayedMessage | async}}', - }) - class MyComponent { - delayedMessage:Promise