Last active
January 7, 2021 13:58
-
-
Save jhades/05d6021c271ebeaf5ce3a61ff8d8f44a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector:'app', | |
template: ` | |
<button (click)="save()">Save Lesson</button> | |
<lessons-list [lessons]="lessons"></lessons-list> | |
` | |
}) | |
export class App { | |
constructor(private lessonsService: LessonsService) { | |
} | |
save() { | |
this.lessonsService.createLesson("Introduction to Components"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class LessonsService { | |
constructor(private http: Http) { | |
} | |
createLesson(description) { | |
const headers = new Headers(); | |
headers.append('Content-Type', 'application/json; charset=utf-8'); | |
headers.append('X-Requested-With','XMLHttpRequest'); | |
this.http.post('/lessons', JSON.stringify({description}), headers); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector:'app', | |
template: `...` | |
}) | |
export class App { | |
constructor(private lessonsService: LessonsService) { | |
} | |
save() { | |
this.lessonsService.createLesson("Introduction to Components") | |
.subscribe( | |
() => console.log('lesson saved successfully'), | |
console.error | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from "@angular/core"; | |
import {Http, Headers} from "@angular/http"; | |
import {xhrHeaders} from "./xhr-headers"; | |
@Injectable() | |
export class LessonsService { | |
constructor(private http: Http) { | |
} | |
createLesson(description) { | |
const headers = new Headers(); | |
headers.append('Content-Type', 'application/json; charset=utf-8'); | |
headers.append('X-Requested-With','XMLHttpRequest'); | |
return this.http.post('/lessons', JSON.stringify({description}), headers); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class LessonsService { | |
constructor(private http: Http) { | |
} | |
loadLessons() { | |
return this.http.get('/lessons'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector:'app', | |
template: ` | |
<button (click)="save()">Save Lesson</button> | |
<lessons-list [lessons]="lessons$ | async"></lessons-list> | |
` | |
}) | |
export class App implements OnInit { | |
lessons$: Observable<Lesson[]>; | |
constructor(private lessonsService: LessonsService) { | |
} | |
ngOnInit() { | |
this.lessons$ = this.lessonsService.loadLessons(); | |
this.lessons$.subscribe( | |
() => console.log('lessons loaded'), | |
console.error | |
); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ngOnInit() { | |
this.lessons$ = this.lessonsService.loadLessons().pipe(shareReplay()); | |
this.lessons$.subscribe( | |
() => console.log('lessons loaded'), | |
console.error | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Resolve, ActivatedRouteSnapshot, RouterStateSnapshot} from "@angular/router"; | |
import {Lesson} from "./lesson"; | |
import {Observable} from "rxjs/Rx"; | |
import {Injectable} from "@angular/core"; | |
import {LessonsService} from "./lessons.service"; | |
@Injectable() | |
export class LessonResolver implements Resolve<Lesson> { | |
constructor(private lessonsService: LessonsService) { | |
} | |
resolve(route:ActivatedRouteSnapshot, | |
state:RouterStateSnapshot):Observable<Lesson> { | |
return this.lessonsService | |
.findLessonByUrl(route.params['id']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
path: 'lessons/:id', | |
children: [ | |
{ | |
path: 'edit', | |
component: EditLessonComponent, | |
resolve: { | |
lesson: LessonResolver | |
} | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resolve(route:ActivatedRouteSnapshot, state:RouterStateSnapshot):Observable<Lesson> { | |
return this.lessonsService | |
.findLessonByUrl(route.params['id']) | |
.first(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment