admin管理员组文章数量:1356845
I am working with NgRx Signal Store in an Angular project, and I need to make one store DashboardStore
reactively call a method when a property in another store FilterSelectionStore
updates.
Use Case:
FilterSelectionStore
maintains a selectedFilters
property, which i am updating whenever there is a change in filter selection
DashboardStore
has a method loadDrivers
, which should be executed whenever selectedFilters
in FilterSelectionStore
updates.
When selectedFilters
updates in FilterSelectionStore
, loadDrivers
is not re-executed.How can I make DashboardStore
reactively respond when selectedFilters
in FilterSelectionStore
updates?
Current Implementation:
I have DashboardStore
defined like this:
type TDashboardState = {
drivers: TDriver[];
};
const initialState: TDashboardState = {
drivers: []
};
export const DashboardStore = signalStore(
// Initializes the store state
withState(initialState),
// Defines methods for loading drivers based on filters
withMethods((store) => {
const _driverService = inject(DriverService);
return {
loadDrivers: rxMethod<TSelectedFilters>(
pipe(
filter(({ anization, state, zones }) => !!anization && !!state && !!zones?.length),
distinctUntilChanged(isEqual),
switchMap(({ zones }) =>
_driverService.fetchDrivers({ zones: zones! }).pipe(
tapResponse<TDriver[], TResponse>({
next: (operationResult) => {
patchState(store, { drivers: operationResult });
},
error: () => {
patchState(store, { drivers: [] });
}
})
)
)
)
)
};
}),
// Hooks to watch for changes in another store
withHooks((store) => {
const _filterStore = inject(FilterSelectionStore);
return {
onInit() {
// Load drivers on store initialization
store.loadDrivers(_filterStore.selectedFilters());
}
};
})
);
本文标签:
版权声明:本文标题:angular - How to trigger a method in one NgRx Signal Store when a property in another store updates? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743943160a2565935.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论