Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Last active December 28, 2019 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasBurleson/d245aba022646bc28223fbb46cdd5e76 to your computer and use it in GitHub Desktop.
Save ThomasBurleson/d245aba022646bc28223fbb46cdd5e76 to your computer and use it in GitHub Desktop.
Compressing View Logic with Facades
/**
*
* Snippet from blog article: https://auth0.com/blog/ngrx-facades-pros-and-cons/
*
*/
@Component({
selector: 'abl-books-page',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<mat-card>
<mat-card-title>My Collection</mat-card-title>
<mat-card-actions>
<button mat-button raised color="accent" (click)="logout()">Logout</button>
</mat-card-actions>
</mat-card>
<abl-book-preview-list [books]="books$ | async"></abl-book-preview-list>
})
export class BooksPageComponent implements OnInit {
books$: Observable<Book[]>;
constructor(private booksFacade: BooksFacadeService) {
this.books$ = booksFacade.allBooks$;
}
ngOnInit() {
this.booksFacade.dispatch(new BooksPageActions.Load());
}
}
@alex-okrushko
Copy link

alex-okrushko commented Jan 17, 2019

export class BooksPageComponent implements OnInit {
  books$: Observable<Book[]> = this.facade.allBooks$;

  constructor(private facade: BooksFacadeService) {  }
}

vs

export class BooksPageComponent implements OnInit {
  books$: Observable<Book[]> = this.store.select(getAllBooks);

  constructor(private store: Store<{}>) {  }
}

How is it any different other than another layer of BooksFacadeService which literally does the same thing.

View components never know about this complexity.

View components never know about the complexity behind selectors as well - they just consume them. Selectors are already the abstraction and encapsulation layer.

@markgoho
Copy link

I would've done

export class BooksPageComponent {
  constructor(public facade: BooksFacadeService) {  }
}

and

<abl-book-preview-list [books]="facade.allBooks$ | async"></abl-book-preview-list>

Even simpler! Thanks for bringing us this awesome pattern, Thomas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment