Skip to content

Commit

Permalink
Add { root: true } option support to commit and dispatch. (#6)
Browse files Browse the repository at this point in the history
- Update localDispatch to support options.
- Implement root option on both commit and dispatch.
- Update README.md file (sentence specifying the root option).
  • Loading branch information
gcoguiec authored and ktsn committed Feb 24, 2017
1 parent 5066ae6 commit e2bf8ef
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ The local module object is same as normal Vuex module except to have `name` opti

In local module getters and actions, `getters`, `dispatch` and `commit` is namespaced implicitly. In other words, we do not have to care about effects for other global modules. If you want to use root level getters, you can use them from 4th argument of each getter function or `rootGetters` property of the action context.

To dispatch actions or commit mutations in the global namespace, pass `{ root: true }` as the 3rd argument to `dispatch` and `commit`.

```html
<script>
// Counter.vue
Expand Down
9 changes: 5 additions & 4 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ function mapLocalActions (
// overwrite commit and dispatch to convert
// action and mutation type to prefixed format
const { commit, dispatch } = context
context.commit = function localCommit (type: string | Payload, payload?: any, options?: any) {
context.commit = function localCommit (type: string | Payload, payload?: any, options: Dictionary<any> = {}) {
if (typeof type === 'object') {
options = payload
type = type.type
payload = type
}
return commit(localKey(type, moduleName), payload, options)
return commit(options.root ? type : localKey(type, moduleName), payload, options)
}
context.dispatch = function localDispatch (type: string | Payload, payload?: any) {
context.dispatch = function localDispatch (type: string | Payload, payload?: any, options: Dictionary<any> = {}) {
if (typeof type === 'object') {
options = payload
type = type.type
payload = type
}
return dispatch(localKey(type, moduleName), payload)
return dispatch(options.root ? type : localKey(type, moduleName), payload, options)
}

// expose real getters object as rootGetters
Expand Down
60 changes: 59 additions & 1 deletion test/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,25 @@ describe('Register Local Module', () => {

beforeEach(() => {
store = new Vuex.Store({
state: {
property: null
},
getters: {
rootGetter: () => 'root'
rootGetter: () => 'root',
propertyGetter: (state: any) => state.property
},
mutations: {
baz (state: any, value: any) {
state.property = value
}
},
actions: {
rootAction ({ commit }: any, value: any) {
return new Promise(resolve => {
commit('baz', value)
resolve()
})
}
}
})
})
Expand Down Expand Up @@ -147,4 +164,45 @@ describe('Register Local Module', () => {

store.dispatch('local/' + localModule.name + '/foo')
})

it('should commit to the global store when root option enabled.', done => {
const localModule = {
name: 'test',
state: {
count: 0
},
actions: {
foo ({ commit, rootGetters }: any) {
commit('baz', 'qux', { root: true })
assert(rootGetters.propertyGetter === 'qux')
done()
}
}
}

registerLocalModule(store, ['test'], localModule)

store.dispatch('local/' + localModule.name + '/foo')
})

it('should dispatch to the global store when root option enabled.', done => {
const localModule = {
name: 'test',
state: {
count: 0
},
actions: {
foo ({ dispatch, rootGetters }: any) {
dispatch('rootAction', 'bar', { root: true }).then(() => {
assert(rootGetters.propertyGetter === 'bar')
done()
})
}
}
}

registerLocalModule(store, ['test'], localModule)

store.dispatch('local/' + localModule.name + '/foo')
})
})

0 comments on commit e2bf8ef

Please sign in to comment.