Closed mikecann closed 8 years ago
You have a couple of options. You could subscribe to store A and call an action on store B, eg.
storeA(function(stateA) {
storeB.doSomething(stateA)
})
Or, with Hoverboard 3, you could use Hoverboard.compose to create a store that composes one or more stores into a new store, eg.:
storeB = Hoverboard.compose(storeA, function(stateA) {
return { something: stateA.whatever }
})
I hope either of these help - if not, maybe you could give me a similar code example with what you're trying to do.
Im not sure either of those will do what im looking for.
Suppose I have an AppStore which has a logger as state: { logger }
Now suppose I have a UserStore which has actions like login and log out. This store wants to use the logger but it doesnt have access to the state of AppStore.
Basically how do you access the state? Do you have to do something like this:
// in AppStore.ts
import { Logger } from "./Logging"
export default HoverBoard({
init: (state, initState) => { logger: new Logger() };
});
// in UserStore.ts
import * as appStore from "./AppStore"
var appStoreState = null;
appStore(s => appStoreState = s);
export default HoverBoard({
login: (state, username, password) => appStoreState.logger.debug("logging in");
});
So basically you must add a listener to the app store and keep a reference to its state as it changes? There is no way to do just:
// in UserStore.ts
import * as appStore from "./AppStore"
export default HoverBoard({
login: (state, username, password) => appStoreState.logger.debug("logging in");
});
Perhaps I dont fully understand how compose works ... :P
Ah it looks like in your Todomvc example you are doing the following: https://github.com/jesseskinner/hoverboard-todomvc/blob/master/js/stores/app.js
function update() {
var page = PageStore().page,
all = TodoStore().list,
completed = CompletedStore().list,
active = ActiveStore().list;
AppStore.update(page, all, completed, active);
}
So I guess I can do AppStore().logger ?
It would be awesome if you could update that example BTW so it uses ES6 import syntax and perhaps uses the new compose stuff so I can see how it works :)
Yep that's exactly what I was about to say, you can call the store like AppStore() to get the current state at any time, so yes AppStore().logger will work.
I plan to update all the docs very soon with the 3.0 release. I'll use ES6 imports and fat arrows and other syntax when I do so, I think it really makes Hoverboard a lot more fun to use.
Hi,
How would you go about handling the situation where a sub-store-action has a dependency on something else.
For example I have a logger on my base store state and I want to access it in a sub-store state how do I go about doing that?
Or am I missing something?
Cheers, Mike