From da66f930e8f1e244bc8dc4c08d678beb7d9e3723 Mon Sep 17 00:00:00 2001 From: joelchu Date: Mon, 24 Feb 2020 21:06:44 +0800 Subject: [PATCH] resolve the reactive problem by defining all the namespaces when create the store --- packages/@jsonql/client/package.json | 4 ++-- packages/@jsonql/client/vue/vuex.js | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/@jsonql/client/package.json b/packages/@jsonql/client/package.json index e5cd0edc..6adaa410 100644 --- a/packages/@jsonql/client/package.json +++ b/packages/@jsonql/client/package.json @@ -1,6 +1,6 @@ { "name": "@jsonql/client", - "version": "0.3.1", + "version": "0.3.2", "description": "jsonql js http and socket client for browser with multiple API for different framework", "main": "dist/jsonql-client.cjs.js", "module": "index.js", @@ -48,7 +48,7 @@ "license": "MIT", "dependencies": { "flyio": "^0.6.14", - "jsonql-client": "^1.5.20" + "jsonql-client": "^1.5.21" }, "optionalDependencies": { "@jsonql/ws": "^1.0.11" diff --git a/packages/@jsonql/client/vue/vuex.js b/packages/@jsonql/client/vue/vuex.js index 171e7a66..1877eeb3 100644 --- a/packages/@jsonql/client/vue/vuex.js +++ b/packages/@jsonql/client/vue/vuex.js @@ -14,14 +14,19 @@ const ucword = str => ( * @param {object} contract the contract object * @param {object} jsonqlClient static version * @param {boolean} [prefix=true] add prefix or not + * @return {array} actions, names */ function getActions(contract, jsonqlClient, prefix = true) { + const availableTypes = ['query', 'mutation', 'auth'] let actions = {} - const target = ['query', 'mutation', 'auth'] + let names = {} for (let name in contract) { - if (target.indexOf(name) > -1) { + if (availableTypes.indexOf(name) > -1) { for (let resolverName in contract[name]) { let actionName = prefix === false ? resolverName : name + ucword(resolverName) + // keep the name for export + names[actionName] = null + // define actions actions[actionName] = (ctx, ...args) => { return Reflect.apply(jsonqlClient[name], jsonqlClient, [resolverName].concat(args)) .then(result => { @@ -36,7 +41,7 @@ function getActions(contract, jsonqlClient, prefix = true) { } } } - return actions + return [ actions, names ] } /** @@ -46,14 +51,15 @@ function getActions(contract, jsonqlClient, prefix = true) { * @return {object} the Vuex store object */ function getJsonqlVuexModule(Fly, config) { - const { prefix, contract, debugOn } = config // could be undefined but we want false boolean + const { prefix, contract } = config // could be undefined but we want false boolean const jsonqlClient = jsonqlStaticClient(Fly, config) + const [ actions, names ] = getActions(contract, jsonqlClient, prefix) // just create the structure on the fly return { namespaced: true, state: { - result: {}, - error: {} + result: names, + error: names }, mutations: { addToResult(state, { name, result }) { @@ -64,7 +70,7 @@ function getJsonqlVuexModule(Fly, config) { } }, // because jsonql are all async call, everything will be create as actions - actions: getActions(contract, jsonqlClient, prefix), + actions: actions, getters: { getJsonqlResult: (state) => (name) => { return state.result[name] -- Gitee