Skip to content

Commit

Permalink
docs: add universal guide (angular#18707)
Browse files Browse the repository at this point in the history
- based on original effort in PR 17573

PR Close angular#18707
  • Loading branch information
wardbell authored and chuckjaz committed Oct 9, 2017
1 parent 82b6664 commit f03eaa6
Show file tree
Hide file tree
Showing 36 changed files with 1,200 additions and 336 deletions.
3 changes: 3 additions & 0 deletions aio/content/examples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ aot-compiler/**/*.factory.d.ts
# styleguide
!styleguide/src/systemjs.custom.js

# universal
!universal/**/webpack.config.universal.js

# plunkers
*plnkr.no-link.html

Expand Down
62 changes: 0 additions & 62 deletions aio/content/examples/universal/package.steve.json

This file was deleted.

3 changes: 2 additions & 1 deletion aio/content/examples/universal/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'detail/:id', component: HeroDetailComponent },
{ path: 'heroes', component: HeroesComponent }
{ path: 'heroes', component: HeroesComponent },
{ path: '**', redirectTo: '/dashboard' }
];

@NgModule({
Expand Down
1 change: 0 additions & 1 deletion aio/content/examples/universal/src/app/app.component.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* #docregion */
h1 {
font-size: 1.2em;
color: #999;
Expand Down
2 changes: 0 additions & 2 deletions aio/content/examples/universal/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// #docplaster
// #docregion
import { Component } from '@angular/core';

@Component({
Expand Down
60 changes: 34 additions & 26 deletions aio/content/examples/universal/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,62 @@
// #docplaster
// #docregion
// #docregion v1, v2
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
// #docregion simple
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';

// #enddocregion v1
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';

// #docregion v1
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard.component';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroService } from './hero.service';
// #enddocregion v1, v2
import { HeroSearchComponent } from './hero-search.component';
// #docregion v1, v2

// #enddocregion simple
// #docregion platform-detection
import { PLATFORM_ID, APP_ID, Inject } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';

// #enddocregion platform-detection
// #docregion simple
@NgModule({
imports: [
BrowserModule.withServerTransition({
appId: 'toh-universal'
}),
// #docregion browsermodule
BrowserModule.withServerTransition({ appId: 'uni' }),
// #enddocregion browsermodule
FormsModule,
HttpModule,
// #enddocregion v1
// #docregion in-mem-web-api
InMemoryWebApiModule.forRoot(InMemoryDataService),
// #enddocregion in-mem-web-api
// #docregion v1
HttpClientModule,
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService),
AppRoutingModule
],
// #docregion search
declarations: [
AppComponent,
DashboardComponent,
HeroDetailComponent,
HeroesComponent,
// #enddocregion v1, v2
HeroSearchComponent
// #docregion v1, v2
],
// #enddocregion search
providers: [ HeroService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
export class AppModule {

// #enddocregion simple
// #docregion platform-detection
constructor(
@Inject(PLATFORM_ID) private platformId: Object,
@Inject(APP_ID) private appId: string) {
const platform = isPlatformBrowser(platformId) ?
'on the server' : 'in the browser';
console.log(`Running ${platform} with appId=${appId}`);
}
// #enddocregion platform-detection
// #docregion simple
}
// #enddocregion simple
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* #docregion */
[class*='col-'] {
float: left;
padding-right: 20px;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!-- #docregion -->
<h3>Top Heroes</h3>
<div class="grid grid-pad">
<a *ngFor="let hero of heroes" [routerLink]="['/detail', hero.id]" class="col-1-4">
<a *ngFor="let hero of heroes | async" [routerLink]="['/detail', hero.id]" class="col-1-4">
<div class="module hero">
<h4>{{hero.name}}</h4>
</div>
Expand Down
11 changes: 6 additions & 5 deletions aio/content/examples/universal/src/app/dashboard.component.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// #docregion , search
import { Component, OnInit } from '@angular/core';

import { Hero } from './hero';
import { HeroService } from './hero.service';

import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';

@Component({
selector: 'my-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: [ './dashboard.component.css' ]
})
// #enddocregion search
export class DashboardComponent implements OnInit {
heroes: Hero[] = [];
heroes: Observable<Hero[]>;

constructor(private heroService: HeroService) { }

ngOnInit(): void {
this.heroService.getHeroes()
.then(heroes => this.heroes = heroes.slice(1, 5));
this.heroes = this.heroService.getHeroes()
.map(heroes => heroes.slice(1, 5));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* #docregion */
label {
display: inline-block;
width: 3em;
Expand All @@ -25,6 +24,6 @@ button:hover {
}
button:disabled {
background-color: #eee;
color: #ccc;
color: #ccc;
cursor: auto;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!-- #docregion -->
<div *ngIf="hero">
<h2>{{hero.name}} details!</h2>
<div>
Expand All @@ -8,7 +7,5 @@ <h2>{{hero.name}} details!</h2>
<input [(ngModel)]="hero.name" placeholder="name" />
</div>
<button (click)="goBack()">Back</button>
<!-- #docregion save -->
<button (click)="save()">Save</button>
<!-- #enddocregion save -->
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// #docregion
import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';
Expand Down Expand Up @@ -27,12 +26,11 @@ export class HeroDetailComponent implements OnInit {
.subscribe(hero => this.hero = hero);
}

// #docregion save
save(): void {
this.heroService.update(this.hero)
.then(() => this.goBack());
this.heroService
.update(this.hero)
.subscribe(() => this.goBack());
}
// #enddocregion save

goBack(): void {
this.location.back();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* #docregion */
.search-result{
border-bottom: 1px solid gray;
border-left: 1px solid gray;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!-- #docregion -->
<div id="search-component">
<h4>Hero Search</h4>
<input #searchBox id="search-box" (keyup)="search(searchBox.value)" />
Expand Down
16 changes: 1 addition & 15 deletions aio/content/examples/universal/src/app/hero-search.component.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
// #docplaster
// #docregion
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

// #docregion rxjs-imports
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';

// Observable class extensions
import 'rxjs/add/observable/of';

// Observable operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
// #enddocregion rxjs-imports

import { HeroSearchService } from './hero-search.service';
import { Hero } from './hero';
Expand All @@ -26,27 +20,20 @@ import { Hero } from './hero';
providers: [HeroSearchService]
})
export class HeroSearchComponent implements OnInit {
// #docregion search
heroes: Observable<Hero[]>;
// #enddocregion search
// #docregion searchTerms
private searchTerms = new Subject<string>();
// #enddocregion searchTerms

constructor(
private heroSearchService: HeroSearchService,
private router: Router) {}
// #docregion searchTerms

// Push a search term into the observable stream.
search(term: string): void {
this.searchTerms.next(term);
}
// #enddocregion searchTerms
// #docregion search

ngOnInit(): void {
this.heroes = this.searchTerms
this.heroes = this.searchTerms.asObservable()
.debounceTime(300) // wait 300ms after each keystroke before considering the term
.distinctUntilChanged() // ignore if next search term is same as previous
.switchMap(term => term // switch to new observable each time the term changes
Expand All @@ -60,7 +47,6 @@ export class HeroSearchComponent implements OnInit {
return Observable.of<Hero[]>([]);
});
}
// #enddocregion search

gotoDetail(hero: Hero): void {
let link = ['/detail', hero.id];
Expand Down
24 changes: 16 additions & 8 deletions aio/content/examples/universal/src/app/hero-search.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// #docregion
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Inject, Injectable, Optional } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

import { Hero } from './hero';
import { Hero } from './hero';

// #docregion class
@Injectable()
export class HeroSearchService {

constructor(private http: Http) {}
private searchUrl = 'api/heroes/?name='; // URL to web api

constructor(
private http: HttpClient,
@Optional() @Inject(APP_BASE_HREF) origin: string) {
this.searchUrl = (origin || '') + this.searchUrl;
}

search(term: string): Observable<Hero[]> {
return this.http
.get(`api/heroes/?name=${term}`)
.map(response => response.json().data as Hero[]);
.get(this.searchUrl + term)
.map((data: any) => data.data as Hero[]);
}
}
// #enddocregion class
Loading

0 comments on commit f03eaa6

Please sign in to comment.