From 33b61977b2cd0bc35b257ce526d5dd2fd460cd10 Mon Sep 17 00:00:00 2001 From: Ward Bell Date: Thu, 3 Nov 2016 01:37:55 -0700 Subject: [PATCH] docs(ts-to-js): ward's edits (incomplete) --- .../js-es6-decorators/app/hero.component.es6 | 3 +- .../cb-ts-to-js/js-es6/app/hero.component.es6 | 4 +- .../cb-ts-to-js/js/app/hero-dsl.component.js | 7 +- .../cb-ts-to-js/js/app/hero.component.js | 9 +- .../cb-ts-to-js/ts/app/hero.component.ts | 3 +- public/docs/dart/latest/cookbook/_data.json | 2 +- public/docs/js/latest/cookbook/_data.json | 2 +- public/docs/js/latest/cookbook/ts-to-js.jade | 491 +--------------- public/docs/ts/latest/cookbook/_data.json | 2 +- public/docs/ts/latest/cookbook/ts-to-js.jade | 546 +++++++++++++++++- public/docs/ts/latest/guide/change-log.jade | 6 + 11 files changed, 565 insertions(+), 510 deletions(-) diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero.component.es6 index 4b4ba8529d..83dc6d409d 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero.component.es6 +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero.component.es6 @@ -4,8 +4,7 @@ import { Component } from '@angular/core'; @Component({ selector: 'hero-view', - template: - '

Hero: {{getName()}}

' + template: '

{{title}}: {{getName()}}

' }) // #docregion appexport // #docregion class diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 index 4b5c20057a..c998d31a6f 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 @@ -5,7 +5,7 @@ import { Component } from '@angular/core'; // #docregion class // #docregion appexport export class HeroComponent { - construct() { + constructor() { this.title = 'Hero Detail'; } getName() {return 'Windstorm'; } @@ -16,7 +16,7 @@ export class HeroComponent { HeroComponent.annotations = [ new Component({ selector: 'hero-view', - template: '

Hero: {{getName()}}

' + template: '

{{title}}: {{getName()}}

' }) ]; // #enddocregion metadata diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js index a073138cff..6d53bc2850 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js @@ -5,14 +5,13 @@ // #docregion component var HeroComponent = ng.core.Component({ selector: 'hero-view-2', - template: '

Name: {{getName()}}

', + template: '

{{title}}: {{getName()}}

', }) .Class({ constructor: function() { + this.title = "Hero Detail"; }, - getName: function() { - return 'Windstorm'; - } + getName: function() { return 'Windstorm'; } }); // #enddocregion component diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js index 745a450096..ec5b706624 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js @@ -9,19 +9,16 @@ function HeroComponent() { this.title = "Hero Detail"; } - + HeroComponent.prototype.getName = function() { return 'Windstorm'; }; // #enddocregion constructorproto + // #enddocregion appexport HeroComponent.annotations = [ new ng.core.Component({ selector: 'hero-view', - template: '

Hero: {{getName()}}

' + template: '

{{title}}: {{getName()}}

' }) ]; - // #docregion constructorproto - - HeroComponent.prototype.getName = function() {return 'Windstorm';}; - // #enddocregion constructorproto // #enddocregion metadata app.HeroesModule = diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero.component.ts index 4b4ba8529d..83dc6d409d 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero.component.ts @@ -4,8 +4,7 @@ import { Component } from '@angular/core'; @Component({ selector: 'hero-view', - template: - '

Hero: {{getName()}}

' + template: '

{{title}}: {{getName()}}

' }) // #docregion appexport // #docregion class diff --git a/public/docs/dart/latest/cookbook/_data.json b/public/docs/dart/latest/cookbook/_data.json index a747acd83f..eb1a46ace4 100644 --- a/public/docs/dart/latest/cookbook/_data.json +++ b/public/docs/dart/latest/cookbook/_data.json @@ -60,7 +60,7 @@ "ts-to-js": { "title": "TypeScript to JavaScript", - "intro": "Convert Angular TypeScript examples into ES5 JavaScript", + "intro": "Convert Angular TypeScript examples into ES6 and ES5 JavaScript", "hide": true }, diff --git a/public/docs/js/latest/cookbook/_data.json b/public/docs/js/latest/cookbook/_data.json index 3f319736d6..74ee2a0be9 100644 --- a/public/docs/js/latest/cookbook/_data.json +++ b/public/docs/js/latest/cookbook/_data.json @@ -55,7 +55,7 @@ "ts-to-js": { "title": "TypeScript to JavaScript", - "intro": "Convert Angular TypeScript examples into ES5 JavaScript" + "intro": "Convert Angular TypeScript examples into ES6 and ES5 JavaScript" }, "visual-studio-2015": { diff --git a/public/docs/js/latest/cookbook/ts-to-js.jade b/public/docs/js/latest/cookbook/ts-to-js.jade index 1ed9ac2489..77e3f9b7d6 100644 --- a/public/docs/js/latest/cookbook/ts-to-js.jade +++ b/public/docs/js/latest/cookbook/ts-to-js.jade @@ -1,490 +1 @@ -include ../../../../_includes/_util-fns - -:marked - Everything that we can do in Angular in TypeScript, we can also do - in JavaScript. Translating from one language to the other is mostly a - matter of changing the way we organize our code and the way we access - Angular APIs. - - Since TypeScript is a popular language option in Angular, many of the - code examples you see on the Internet as well as on this site are written - in TypeScript. This cookbook contains recipes for translating these kinds of - code examples to ES5, so that they can be applied to Angular JavaScript - applications. - - -:marked - ## Table of contents - - [From TS to ES6 to ES5](#from-ts) - - [Modularity: imports and exports](#modularity) - - [Classes and Class Metadata](#class-metadata) - - [Input and Output Metadata](#property-metadata) - - [Dependency Injection](#dependency-injection) - - [Host and Query Metadata](#host-query-metadata) - - **Run and compare the live TypeScript and JavaScript - code shown in this cookbook.** - -a(id="from-ts") -.l-main-section -:marked - ## From TS to ES6 to ES5 - - Since TypeScript is a superset of ES6 JavaScript, and ES6 itself is a superset of ES5, the - transformation of Typescript code all the way to ES5 javascript can be seen as "shedding" - features. - - When going from TypeScript to ES6 with decorators, we mostly remove - [type annotations](https://www.typescriptlang.org/docs/handbook/basic-types.html) - , such as `string` or `boolean`, and - [class property modifiers](http://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers) - such as `public` and `private`. - The exception is type annotations used for dependency injection, which can be kept. - - Going from ES6 with decorators to plain ES6 JavaScript we lose all - [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) - and the remaining type annotations. - We also lose class properties, which now have to be declared in the class constructor. - - Finally, in the transition from ES6 to ES5 JavaScript the main missing features are `import` - statements and `class` declarations. - - For ES6 transpilation we recommend using a similar setup as our TypeScript quickstart, - replacing TypeScript with [Babel](https://babeljs.io/) using the `es2015` preset. - To use decorators and annotations with Babel, install the - [`angular2`](https://github.com/shuhei/babel-plugin-angular2-annotations) preset as well. - -a(id="modularity") -.l-main-section -:marked - ## Importing and Exporting - - ### Importing Angular Code - - In TypeScript and ES6 JavaScript, Angular classes, functions, and other members are imported - with ES6 `import` statements. - - In ES5 JavaScript code, when using [the Angular packages](../glossary.html#!#scoped-package), - we can access Angular code through the global `ng` object. In the nested members of this - object we'll find everything we would import from `@angular` in TypeScript: - -+makeTabs(` - cb-ts-to-js/ts/app/main.ts, - cb-ts-to-js/js-es6-decorators/app/main.es6, - cb-ts-to-js/js-es6/app/main.es6, - cb-ts-to-js/js/app/main.js - `,` - ng2import, - ng2import, - ng2import, - ng2import - `,` - Typescript, - ES6 JavaScript with decorators, - ES6 JavaScript, - ES5 JavaScript - `) - -:marked - ### Importing and Exporting Application Code - - Each file in an Angular TypeScript or ES6 JavaScript application constitutes a ES6 module. - When we want to make something from a module available to other modules, we `export` it. - - In an Angular ES5 JavaScript application, we load each file to the page using a `