Vuexed Objects
Motivation
Suppose that you have a list of objects in your Vuex Store:
const store = state: objects: field: value // ...
You need to modify these values, but the store is not updated when you set the value normally:
storestateobjectsifield = newValue
Or maybe you want to use the field in v-model
:
<template> <component v-model="$store.state.objects[i].field /></template>
This is especially annoying when you are using plugins that rely on mutation subscriptions to work, such as vuex-persistedstate.
The official vuex guide recommends to use a 2-way computed property, which does work, but still requires a lot of boilerplate code. The computed properties could be mixed in but you would still have to write mutations for each field.
There are existing libraries that try to fix this problem such as vuex-map-fields. However they reuse a single mutation for multiple fields and then you lose the benefit of verbosity and nice devtool features. Additionally, this approach does not work when setting the field outside a Vue component.
Goal
What this library tries to accomplish is a way to have all the benefits of regular vuex mutations (such as verbosity, time travel, reverting commits) and none of the burden of boilerplate code.
It makes sense to group similarly structured objects into their own module but this library does not enforce such groupings. You are responsible for grouping objects together with informative module names.
This library tries to be as configurable as possible so that it can be integrated into as many different environments as possible.
How It Works
For each field in an object, a mutation is created and a custom setter is created which invokes that mutation. A module is then built with those generated mutations and the objects at its state.
Each generated mutation is just an empty arrow function, but the value being set on the object's field is still passed to the commit function as the payload so that devtool features work as expected.
The custom setter respects any previous custom setters so it should not interfere with Vue's observer functionality.
A single additional mutation is added to the module which can be used to add objects to the module. Objects added in this manner also have their enumerable properties' setters replaced with custom setters which invoke the appropriate mutations.
In order to support properties that were not present on the objects at the initial module creation, a hotUpdateModule
function can be passed in which will be invoked with the updated module in the case where additional mutations are added. This updated module can be passed to the Vuex store's hotUpdate
function to achieve a dynamic module.
Installation
NPM
npm install vuexed-objects --save
yarn
yarn add vuexed-objects
CDN
Usage
You are responsible for adding the module to the store yourself. The main entrypoint is the function buildModule
but addReactivity
is also exported in the hope that it will be useful.
Static Modules
The simplest way to use this library is to build a static module. A static module will only have the mutations created at the initial call to buildModule
. Adding objects will make them reactive but only for those initial mutations.
Static registration
const obj0 = a: 5 b: 2const obj1 = a: 7 b: 6 // declare the variable up here so that it is available in the correct scopelet store store = state mutations actions getters modules: ...modules myModuleName: obj0a = 1 // will trigger mutation myModuleName/a with payload {value: 1} const obj2 = a: 1 b: 0 c: 3 // we can add objects to the module with a special mutationstore obj2b = 4 // will trigger mutation myModuleName/b with payload {value: 4}obj2c = 2 // will not trigger any mutations since we used a static module
Dynamic registration with nested module
// ... store
Dynamic Modules
Its possible to have mutations added to the module as objects when new properties are added. You must pass in hotModuleUpdate
to buildModule
in order to enable this feature. hotModuleUpdate
will be called when new mutations are added with the updated module passed in as the only argument.
You need to update the store (presumably using store.hotUpdate
) to use the updated module. Special care must be taken when using this in conjunction with hot module replacement. You need to make sure that the module that you are passing into store.hotUpdate
in your HMR function is always the updated version.
const obj0 = a: 5 b: 2const obj1 = a: 7 b: 6 // declare the variables up here so that they are available in the correct scopelet store vuexedModule vuexedModule = store = state mutations actions getters modules: ...modules myModuleName: vuexedModule if modulehot modulehotaccept './getters' './actions' './mutations' './modules' { store } obj0a = 1 // will trigger mutation myModuleName/a with payload {value: 1} const obj2 = a: 1 b: 0 c: 3const obj3 = c: 4 d: 5 // we can add objects to the module with a special mutationstore obj2b = 4 // will trigger mutation myModuleName/b with payload {value: 4}obj2c = 2 // will now trigger myModuleName/c since we used dynamic module
API
See API.md
Caveats
Nested objects will not vuexed. You must explicitly call buildModule
for each list of objects that you want to be vuexed. This may be added in a future release.
Contributing
Feedback and merge requests are welcome!