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

docs(toh): Replaced window.history with location service #2439

Merged
merged 1 commit into from
Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions public/docs/_examples/toh-5/ts/app/hero-detail.component.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// #docregion added-imports
// Keep the Input import for now, we'll remove it later:
import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';

import { HeroService } from './hero.service';
// #enddocregion added-imports
Expand All @@ -20,8 +21,9 @@ export class HeroDetailComponent implements OnInit {

constructor(
private heroService: HeroService,
private route: ActivatedRoute) {
}
private route: ActivatedRoute,
private location: Location
) {}

ngOnInit() {}
}
14 changes: 8 additions & 6 deletions public/docs/_examples/toh-5/ts/app/hero-detail.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// #docplaster
// #docregion , v2
import { Component, OnInit } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';

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

@Component({
selector: 'my-hero-detail',
Expand All @@ -22,8 +23,9 @@ export class HeroDetailComponent implements OnInit {
// #docregion ctor
constructor(
private heroService: HeroService,
private route: ActivatedRoute) {
}
private route: ActivatedRoute,
private location: Location
) {}
// #enddocregion ctor

// #docregion ngOnInit
Expand All @@ -38,7 +40,7 @@ export class HeroDetailComponent implements OnInit {

// #docregion goBack
goBack(): void {
window.history.back();
this.location.back();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

}
// #enddocregion goBack
}
12 changes: 7 additions & 5 deletions public/docs/_examples/toh-6/ts/app/hero-detail.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// #docregion
import { Component, OnInit } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';

import { Hero } from './hero';
import { HeroService } from './hero.service';
Expand All @@ -15,8 +16,9 @@ export class HeroDetailComponent implements OnInit {

constructor(
private heroService: HeroService,
private route: ActivatedRoute) {
}
private route: ActivatedRoute,
private location: Location
) {}

ngOnInit(): void {
this.route.params.forEach((params: Params) => {
Expand All @@ -29,11 +31,11 @@ export class HeroDetailComponent implements OnInit {
// #docregion save
save(): void {
this.heroService.update(this.hero)
.then(this.goBack);
.then(() => this.goBack());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? You are replacing a function g by an anonymous function which does nothing but call g. I.e., g and () => g() are equivalent, though the former is shorter.

Copy link
Contributor Author

@brandonroberts brandonroberts Sep 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It throws an error of Cannot read property 'location' of nulll error when just providing the callback function when the e2e tests ran. I can use this.goBack.bind(this) instead of you think that would be cleaner.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, your change is actually the right thing to do to ensure that this gets bound properly in goBack.

}
// #enddocregion save

goBack(): void {
window.history.back();
this.location.back();
}
}
11 changes: 6 additions & 5 deletions public/docs/ts/latest/tutorial/toh-pt5.jade
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ figure.image-display
img(src='/resources/images/devguide/toh/nav-diagram.png' alt="View navigations")

:marked
We'll add Angular’s *Component Router* to our app to satisfy these requirements.
We'll add Angular’s *Router* to our app to satisfy these requirements.

.l-sub-section
:marked
Expand Down Expand Up @@ -166,7 +166,7 @@ block app-comp-v1
Instead of displaying heroes automatically, we'd like to show them *after* the user clicks a button.
In other words, we'd like to navigate to the list of heroes.

We'll need the Angular *Component Router*.
We'll need the Angular *Router*.

block angular-router
:marked
Expand Down Expand Up @@ -523,7 +523,7 @@ block route-params

- var _ActivatedRoute = _docsFor == 'dart' ? 'RouteParams' : 'ActivatedRoute'
:marked
Let's have the `!{_ActivatedRoute}` service and the `HeroService` injected
Let's have the `!{_ActivatedRoute}` service, the `HeroService` and the `Location` service injected
into the constructor, saving their values in private fields:

+makeExcerpt('app/hero-detail.component.ts (constructor)', 'ctor')
Expand Down Expand Up @@ -569,7 +569,8 @@ block extract-id
How do we navigate somewhere else when we're done?

The user could click one of the two links in the `AppComponent`. Or click the browser's back button.
We'll add a third option, a `goBack` method that navigates backward one step in the browser's history stack.
We'll add a third option, a `goBack` method that navigates backward one step in the browser's history stack
using the `Location` service we injected previously.

+makeExcerpt('app/hero-detail.component.ts', 'goBack')

Expand Down Expand Up @@ -901,7 +902,7 @@ block file-tree-end

We travelled a great distance in this chapter

- We added the Angular *Component Router* to navigate among different components.
- We added the Angular *Router* to navigate among different components.
- We learned how to create router links to represent navigation menu items.
- We used router link parameters to navigate to the details of user selected hero.
- We shared the `HeroService` among multiple components.
Expand Down