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

Declare parent module of local modules automatically #1

Merged
merged 2 commits into from
Oct 15, 2016
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
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