Skip to content

Commit

Permalink
Merge pull request #1 from ktsn/register-parent
Browse files Browse the repository at this point in the history
Declare parent module of local modules automatically
  • Loading branch information
ktsn authored Oct 15, 2016
2 parents 37d8604 + 3e55682 commit c656f72
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ Vue.use(VuexLocal, {
parentModulePath: ['locals']
})

export default new Vuex.Store({
modules: {
// make sure to define the module that is
// specified by `parentModulePath` option
locals: {}
}
})
// generate `locals` module automatically
export default new Vuex.Store({})
```

Then, you can define a local module on each component. The component option will have `local` property that is a function returning a local module object.
Expand Down
16 changes: 16 additions & 0 deletions src/mixin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as _Vue from 'vue'
import { ComponentOptions } from 'vue'
import { Store } from 'vuex'
import { PluginOptions, VuePrivate } from './declarations.d'

import { registerLocalModule, unregisterLocalModule } from './register'
Expand All @@ -21,6 +22,8 @@ export function applyMixin (

assert(this.$store, 'store must be injected')

ensureParent(this.$store, parentModulePath)

const localModule = this.$options.local.call(this)

let name = localModule.name
Expand All @@ -43,3 +46,16 @@ export function applyMixin (
}
} as ComponentOptions<_Vue & VuePrivate>)
}

function ensureParent (store: Store<any>, parentPath: string[]): void {
let state = store.state
const currentPath: string[] = []
parentPath.forEach(key => {
state = state[key]
currentPath.push(key)

if (typeof state === 'undefined') {
store.registerModule(currentPath, {})
}
})
}
45 changes: 40 additions & 5 deletions test/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,54 @@ import { applyMixin } from '../src/mixin'
Vue.use(Vuex)

describe('Global Mixin', () => {
let store: Vuex.Store<any>

applyMixin(Vue, {
parentModulePath: ['local']
})

beforeEach(() => {
store = new Vuex.Store({
it('auto declare parent module', () => {
const store: Vuex.Store<any> = new Vuex.Store({})

new Vue({
store,
local: () => ({
name: 'test',
state: { value: 0 }
})
})

assert(typeof store.state.local === 'object')
})

it('does not touch existing parent module', () => {
const store: Vuex.Store<any> = new Vuex.Store({
modules: {
local: {}
local: {
modules: {
foo: {
state: {
value: 'foo'
}
}
}
}
}
})

new Vue({
store,
local: () => ({
name: 'test',
state: { value: 'test' }
})
})

assert(store.state.local.foo.value === 'foo')
assert(store.state.local.test.value === 'test')
})

it('binds local module', () => {
const store: Vuex.Store<any> = new Vuex.Store({})

const vm: any = new Vue({
store,
local: () => ({
Expand Down Expand Up @@ -55,6 +88,8 @@ describe('Global Mixin', () => {
})

it('remove local module when component is destroyed', () => {
const store: Vuex.Store<any> = new Vuex.Store({})

const vm: any = new Vue({
store,
local: () => ({
Expand Down

0 comments on commit c656f72

Please sign in to comment.