Vuex: New module option `subStore`

Created on 9 Oct 2016  路  4Comments  路  Source: vuejs/vuex

This proposal is come up from the discussion in #359.

Summary of proposal

Modules will have subStore option that expects a sub store name. If the option is specified, a sub store will be created under store.subStores.

The sub store has state, getters, dispatch and commit which are same interface as Vuex.Store but state will be the local state of corresponding module.

If prefix option (another proposal #380) is specified, sub store will prefix all getters, actions and mutations types implicitly. For example, if we set prefix: 'user/' on sub store module, subStore.dispatch('login') will be transformed to rootStore.dispatch('user/login').

Why is this worth adding?

Currently, we need to collect state, getters, actions and mutations from global namespace when we use them on components even though we separate the application state and logics to each module.

It would be good to provide the sub store because we can load all assets in a module at once and use them without prefix.

Examples

Module definition:

const userModule = {
  subStore: 'user',
  prefix: 'userModule/'

  // module assets
  state: { ... },
  getters: {
    isAdmin () { ... }
  },
  actions: {
    login () { ... }
  },
  mutations: {
    setName () { ... },
  }
}

Usage of sub store:

const store = new Vuex.Store({
  modules: {
    user: userModule
  }
})

const user = store.subStores.user
user.state.id // -> store.state.user.id
user.getters.isAdmin // -> store.getters['userModule/isAdmin']
user.dispatch('login') // -> store.dispatch('userModule/login')
user.commit('setName') // -> store.commit('userModule/setName')

Component binding:

computed: {
  // 2nd argument will be sub store name
  ...mapGetters({
    admin: 'isAdmin' // -> admin: 'userModule/isAdmin'
  }, 'user')
}
discussion proposal

Most helpful comment

@yyx990803
I think this feature is not overlap with modules because modules let us divide the concern on _definition time_ but they are flatten when we use them. So we need to write prefix for all execution of getters, actions and mutation. This is what the sub store would solves.

@psi-4ward
No, vuex will be still single source of truth. Sub stores are just alias of state, getters, actions and mutations for the root store. Maybe this name is a little bad...

All 4 comments

I think this has overlaps with modules in terms of functionality and makes things more complex than they should be. I'm not convinced about the benefits vs. the extra complexity.

So I currently try to get my head around the vuex stuff (hard btw).
As I understood its the single-source-of-truth so splitting it up results back in multie-sources-of-truth?

@yyx990803
I think this feature is not overlap with modules because modules let us divide the concern on _definition time_ but they are flatten when we use them. So we need to write prefix for all execution of getters, actions and mutation. This is what the sub store would solves.

@psi-4ward
No, vuex will be still single source of truth. Sub stores are just alias of state, getters, actions and mutations for the root store. Maybe this name is a little bad...

I drop this proposal because v2.1's mapXXX helpers simply solve the original issue 馃檪

Was this page helpful?
0 / 5 - 0 ratings