Sub Stores
Ability to create stores within stores. https://gist.github.com/amcdnl/6998c0dbad7af5f424dc7719527f9fb8
One idea is to create dot notation with store names like:
@Store({
name: 'cart'
})
export class CartStore{}
@Store({
name: 'cart.items'
})
export class CartItemsStore {}
then you hook it up like:
@NgModule({
imports: [
NgxsModule.forRoot([CartStore, CartItemsStore])
]
})
export class MyModule {}
NgModule misses the providers:[CartStore, CartItemsStore] even though that's not the point here
You dont have to here, we handle the providers for you automatically.
Good point
A more general remark from an ngrx/store guy: shouldn't the @Store definition doesn't declare a State-like set of properties? Or would that be a purpose of the "defaults" property?
You use defaults property to handle this:
@Store({
name: 'foo',
defaults: { cart: [] }
})
Ok, then I like the idea :)
I like the idea. I still worry about using strings though
We can't use nested objects like that because we can't traverse them to hook them up as providers in the forRoot/forFeature methods because of AoT tracability.
This would be cool but NOT doable.
@Store({
name: 'cart',
defaults: {
items: ItemsStore
}
})
Perhaps change the name of defaults if we were to do that ^^
or
@Store({
name: 'cart',
defaults: {},
stores: {
items: ItemsStore
}
})
Could web make Store a generic?
@Store<MyStateModel>({
name: 'cart',
defaults: {}, // this would adhere to the model passed in
})
I don't follow how you are defining a sub store there
the only thing I am talking about is when you use strings there is no type safety
This is a tad more verbose but maybe it answers the riddle:
@Store<Cart>({
state: (state) => state.cart,
defaults: {}
})
export class Cart {}
@Store<CartList>({
state: (state) => state.cart.list,
defaults: {}
})
export class CartList {}
Not sure on implementation, but just tossing out ideas.
hmmmmmmm. I is intrigued
Essentially treating each store state input like a select. I thought it was clever
state: (state) => state.cart,
It's not completely obvious from what piece of the state it is getting the cart. Is it the app state?
Just to be a bit clear on this, what problem we are trying to solve with sub stores as opposed to having multiple stores are the root level? we should keep in mind that a feature may be lazy loaded / unloaded so it's store (or sub store) may need to be discarded.
What about this:
@Store<Cart>({
name: 'cart',
defaults: {},
children: {
items: CartList
}
})
export class Cart {}
@Store<CartList>({
name: 'list'
defaults: []
})
export class CartList {}
Similar to what vuex does - https://vuex.vuejs.org/en/modules.html
Proposal:
@State<CartState>({
selector: (state: AppState) => state.cart,
defaults: {}
})
export class CartState {
@Action(Checkout)
checkout() {
state.checkedout = true;
}
}
@State<CartItemsState>({
selector: (state: AppState) => state.cart.items,
defaults: {
moo: true
}
})
export class CartItemsState {
@Action(AddtoCart)
addtocart(item) {
state.push(item);
}
}
@amcdnl if someone doesn't care about substates can we make "selector" optional?