From d7c5babf1f66309cedfa4dc3537df82f84d01618 Mon Sep 17 00:00:00 2001 From: Brendan Date: Sun, 25 Dec 2016 21:16:30 +0100 Subject: [PATCH] better vuex store --- examples/vuex-store/pages/index.vue | 5 +++- examples/vuex-store/pages/todos.vue | 41 +++++++++++++++++++++++++++++ examples/vuex-store/store/index.js | 20 ++++---------- examples/vuex-store/store/todos.js | 20 ++++++++++++++ lib/app/index.js | 2 +- lib/app/store.js | 18 +++++++++++++ lib/build/index.js | 4 +++ lib/nuxt.js | 2 +- 8 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 examples/vuex-store/pages/todos.vue create mode 100644 examples/vuex-store/store/todos.js create mode 100644 lib/app/store.js diff --git a/examples/vuex-store/pages/index.vue b/examples/vuex-store/pages/index.vue index 3ebbfdcac6..55131f0fee 100644 --- a/examples/vuex-store/pages/index.vue +++ b/examples/vuex-store/pages/index.vue @@ -1,8 +1,11 @@ diff --git a/examples/vuex-store/pages/todos.vue b/examples/vuex-store/pages/todos.vue new file mode 100644 index 0000000000..7b5d30dfb3 --- /dev/null +++ b/examples/vuex-store/pages/todos.vue @@ -0,0 +1,41 @@ + + + + + diff --git a/examples/vuex-store/store/index.js b/examples/vuex-store/store/index.js index e5ad937c65..a4521c2fc2 100644 --- a/examples/vuex-store/store/index.js +++ b/examples/vuex-store/store/index.js @@ -1,17 +1,7 @@ -import Vue from 'vue' -import Vuex from 'vuex' +export const state = { counter: 0 } -Vue.use(Vuex) - -const store = new Vuex.Store({ - state: { - counter: 0 - }, - mutations: { - increment (state) { - state.counter++ - } +export const mutations = { + increment (state) { + state.counter++ } -}) - -export default store +} diff --git a/examples/vuex-store/store/todos.js b/examples/vuex-store/store/todos.js new file mode 100644 index 0000000000..ca87fe5a3f --- /dev/null +++ b/examples/vuex-store/store/todos.js @@ -0,0 +1,20 @@ +export const state = { + list: [] +} + +export const mutations = { + add (state, { text }) { + state.list.push({ + text, + done: false + }) + }, + + delete (state, { todo }) { + state.list.splice(state.list.indexOf(todo), 1) + }, + + toggle (state, { todo }) { + todo.done = !todo.done + } +} diff --git a/lib/app/index.js b/lib/app/index.js index 64eba583f1..e851d1dc67 100644 --- a/lib/app/index.js +++ b/lib/app/index.js @@ -3,7 +3,7 @@ import Vue from 'vue' import Meta from 'vue-meta' import router from './router.js' -<% if (store) { %>import store from '~store/index.js'<% } %> +<% if (store) { %>import store from './store.js'<% } %> import NuxtChild from './components/nuxt-child.js' import NuxtLink from './components/nuxt-link.js' import Nuxt from './components/nuxt.vue' diff --git a/lib/app/store.js b/lib/app/store.js new file mode 100644 index 0000000000..dc8e030b0f --- /dev/null +++ b/lib/app/store.js @@ -0,0 +1,18 @@ +import Vue from 'vue' +import Vuex from 'vuex' +Vue.use(Vuex) + +const files = require.context('~store', false, /^\.\/.*\.js$/) + +const storeData = { modules: {} } +for (let filename of files.keys()) { + let name = filename.replace(/^\.\//, '').replace(/\.js$/, '') + if (name === 'index') { + Object.assign(storeData, files(filename)) + } else { + storeData.modules[name] = files(filename) + storeData.modules[name].namespaced = true + } +} + +export default new Vuex.Store(storeData) diff --git a/lib/build/index.js b/lib/build/index.js index 4e7a8f15dc..e6adad90c8 100644 --- a/lib/build/index.js +++ b/lib/build/index.js @@ -191,6 +191,10 @@ function * generateRoutesAndFiles () { templatesFiles.push('layouts/default.vue') layouts.default = r(__dirname, 'app', 'layouts', 'default.vue') } + // Add store if needed + if (this.options.store) { + templatesFiles.push('store.js') + } let moveTemplates = templatesFiles.map((file) => { return readFile(r(__dirname, 'app', file), 'utf8') .then((fileContent) => { diff --git a/lib/nuxt.js b/lib/nuxt.js index a3ca5b06c8..cdfa088c92 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -51,7 +51,7 @@ class Nuxt { this.dir = (typeof options.rootDir === 'string' && options.rootDir ? options.rootDir : process.cwd()) this.srcDir = (typeof options.srcDir === 'string' && options.srcDir ? resolve(this.dir, options.srcDir) : this.dir) // If store defined, update store options to true - if (fs.existsSync(join(this.srcDir, 'store', 'index.js'))) { + if (fs.existsSync(join(this.srcDir, 'store'))) { this.options.store = true } // Template