Can Vuex modules watch the state of other modules, and trigger actions consequently?
For example, let's consider the following case:
store.js
import time from './store/time' ;
import position from './store/position' ;
const store = new Vuex.Store
(
{
modules:
{
time,
position
}
}
) ;
store/time.js
export default
{
namespaced: true,
state:
{
time: new Date()
},
getters:
{
time: (aState) => aState.time
},
mutations:
{
setTime (aState, aTime)
{
aState.time = aTime ;
}
}
} ;
store/position.js
export default
{
namespaced: true,
state:
{
positions: {}
},
getters:
{
positions: aState => aState.positions
},
mutations:
{
setPositions (aState, aPositionArray)
{
aState.positions = aPositionArray ;
}
},
actions:
{
fetchPositionsAtTime ({ dispatch, commit, rootGetters }, { time })
{
// Fetch positions at given time from the server
// commit('setPositions', ...)
}
}
} ;
Ideally, I would like the position module to watch the time module, and re-fetch the positions (i.e. trigger fetchPositionsAtTime
) as soon as the time state changes.
Of course, I could add a dispatch to the setTime
mutation to trigger a position module action, but I believe going the other way around (i.e. watching) would be more elegant (as many more modules may require time).
Any solution to this? (without using Vue Component
of course, that is the whole point)