Single Entity with Book Example #144
Replies: 3 comments 4 replies
-
You need to pass an id - |
Beta Was this translation helpful? Give feedback.
-
Nathanel, I am trying to subscribe to the book$ from the book repository and just listen(subscribe) when book$ has been updated.
What I have noticed is that the line in the app.component.ts book$ does not update when a new book is set in the book.repository.ts Is there a different way that I should be listening or subscribing in the app.component.ts for the book? I want to listen for the book to be updated when the store.pipe(selectEntity('s9lymQEACAAJ')); runs and pass that book to any child components. Hopefully this helps with what I am trying to do with the store. Thanks |
Beta Was this translation helpful? Give feedback.
-
I think, every time you reassign the I usually use a const {config, state} = createState(
withProps<{selectedId?: string}>(),
// ...
);
const store = new Store({name: 'bookState', config, state});
@Injectable()
export class BookRepo {
book$ = store.pipe(select(s => s.entities[s.selectedId]));
setBooks(books: Book[], selectedId: string) {
// Every time you change the selected id, the 'book$' will return the according book from the state.
store.update(setEntities(books), s => ({...s, selectedId}));
}
}
@Component({
template: `
Either <div *ngIf="repo.book$ | async as book">{{book.name}}</div>
Or <div>{{(repo.book$ | async)?.name}}</div>
`
})
export class BookComponent {
constructor(public repo: BookRepo) {}
} |
Beta Was this translation helpful? Give feedback.
-
Hi,
I might be missing something pretty easy but how do I setup the store so that component can use the async pipe to get just a single entity (book).
In all the examples I found ->
Here is the modified slackblitz example link
https://stackblitz.com/edit/angular-ivy-kkjzhw?file=src/app/state/books.repository.ts
If I am way off on how to be using the selectEntity or how to setup a component to listen for a single book please let me know.
Thank You
Sean
Beta Was this translation helpful? Give feedback.
All reactions