Skip to content

Instantly share code, notes, and snippets.

@amikitevich
Last active July 21, 2017 00:08
Show Gist options
  • Save amikitevich/9bd22d3a0bf88f1d9833e1611752ce86 to your computer and use it in GitHub Desktop.
Save amikitevich/9bd22d3a0bf88f1d9833e1611752ce86 to your computer and use it in GitHub Desktop.
/**
* service that emulates http
*/
@Injectable()
export class TodoService {
readonly todos = [
{id: 1, text: 'todo1', categoryId: 1},
{id: 2, text: 'todo2', categoryId: 2},
{id: 3, text: 'todo3', categoryId: 1},
{id: 4, text: 'todo4', categoryId: 1}
];
readonly todoCategory = [
{id: 1, name: 'todoCategory1'},
{id: 2, name: 'todoCategory1'}
];
public getTodoItems() {
return Observable.of(this.todos);
}
public getCategory(categoryId: number) {
const category = this.todoCategory.find(item => item.id === categoryId);
return Observable.of(category);
}
}
@Component({
selector: 'todo-list',
template: `<todo-item *ngFor="let item of items$ | async" [todo]="item"></todo-item>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class TodoListComponent implements OnInit {
public items$: Observable<Todo[]>;
constructor(private todoService: TodoService) { }
ngOnInit() {
this.items$: Observable<Todo[]> = this.todoService.getTodoItems();
}
}
@Component({
selector: 'todo-item',
template: `Category: {{categoryName$ | async}}: {{todoText$ | async}}`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class TodoItemComponent implements OnInit {
readonly todo$ = new ReplaySubject<Todo>(1);
public todoCategoryName$: Observable<TodoCategory>;
@Input() set todo(value: Todo) {
this.todo$.next(value);
}
constructor(private todoService: TodoService) { }
ngOnInit() {
this.todoCategoryName$ = this.todo$
.switchMap(todo => this.todoService.getCategory(todo.categoryId))
.pluck('name');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment