Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add { root: true } option support to dispatch. #6

Merged
merged 1 commit into from Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})
})