-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Use *ngrxLet
to create a view model object in the template
#3545
Comments
combineLatest is not the same - it will not emit until every source is emitted at least once. It's not an issue when all the sources are from some store, but it might not always be the case. ngIf map is not quite reliable - in some circumstances, it might ignore the emitted value. I’m from mobile and forgot the exact case - I’ll check later from my laptop. Something related to lifecycle, 100% reproducible. And the point of my comment is that we absolutely need it because alternatives are not good enough! |
I'd like to see the case when Btw, |
Is it possible to make it safe, like
? Then for streams with values, nothing will be changed - they'll emit their values first (BehaviorSubject, or non-empty ReplaySubject). And values for other streams would be presented as "undefined" (and it's true). This way we can know for sure that vm$ will not be stuck. |
@e-oz In this case, all view model properties need to have I'd rather avoid this behavior because the idea for " If the @Component({
selector: 'app-books',
template: `
<ng-container *ngrxLet="{ books: books$, selectedBook: selectedBook$ } as vm">
<!-- 👉 the type of `vm.books` will be `Book[] | undefined` -->
<!-- 👉 `selectedBook$` emits synchronously, so the type of `vm.selectedBook` will be `Book` -->
</ng-container>
`,
})
export class BooksComponent {
readonly selectedBook$ = new BehaviorSubject<Book>(defaultBook);
readonly books$ = this.booksService.getBooks().pipe(startWith(undefined));
} |
Most of my "state" object fields are T|undefined, because I usually have no initial values for them. But maybe it's just me :) But you are right - there are workarounds. |
I really like this idea. A few thoughts that come to mind:
|
Hi @edezekiel, I'm glad you like it. :)
|
Thanks for the response! Based on that I would definitely use ngrxLet except in cases where my component has a facade. When there's a facade I need the typed view model exposed from the facade to the container component. |
Information
The
*ngrxLet
directive could be used to create a view model object in the template as follows:It would be possible to pass a dictionary of observables whose results will be stored in a template variable (
vm
in this example).If an observable name contains the$
suffix, then the suffix will be removed:books$
=>vm.books
books
=>vm.books
EDIT: We decided not to trim the
$
suffix from property names to avoid potential naming collisions.Describe any alternatives/workarounds you're currently using
*ngIf
+async
:The same result can be achieved by using
*ngrxLet
+ngrxPush
.combineLatest
:I would be willing to submit a PR to fix this issue
The text was updated successfully, but these errors were encountered: