Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active January 7, 2021 13:58
Show Gist options
  • Save jhades/05d6021c271ebeaf5ce3a61ff8d8f44a to your computer and use it in GitHub Desktop.
Save jhades/05d6021c271ebeaf5ce3a61ff8d8f44a to your computer and use it in GitHub Desktop.
@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");
}
}
@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);
}
}
@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
);
}
}
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);
}
}
@Injectable()
export class LessonsService {
constructor(private http: Http) {
}
loadLessons() {
return this.http.get('/lessons');
}
}
@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
);
}
}
ngOnInit() {
this.lessons$ = this.lessonsService.loadLessons().pipe(shareReplay());
this.lessons$.subscribe(
() => console.log('lessons loaded'),
console.error
);
}
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']);
}
}
{
path: 'lessons/:id',
children: [
{
path: 'edit',
component: EditLessonComponent,
resolve: {
lesson: LessonResolver
}
}
]
}
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