Simplifies two-way data binding and v-model usage on vuex state, providing full reactivity via generated setters/getters
npm install vuex-dotVue computed properties getters and/or setters generator with the ability to intercept each property change and dispatch vuex action or commit mutation or just hook your own callback. Also you can use plugins.
The main idea of this tool is to have mapState()-like helper with an ability to set additional configuration using chainable methods.
There are some other packages on github -
vuex-map-fields and
vuex-bound for example, but after reading
their docs and sources I've decided to create own package from scratch with such benefits
* lightweight - 3.41 KB after rollup && babel-minify
* flexible - actions dispatching and hooks abilities adds to your code one place for handling reactive changes of target. Plugins support gives you full control of your data flow.
* simple - less code footprint with same features. You need to import only one helper method into your code, which provides a short set of chainable methods for configuring.
* no foreign code injection to your state - no weird logic ("core" mutations, actions, etc) shall be injected into your vuex store, no additional setup of vuex or vm needed. This tool just generates getters and setters, which are done with performance in mind.
* dot notation - with usage of very fast and well tested library get-value
``bash`
npm i vuex-dot
#### State property two way binding (mutation based)
https://codepen.io/anon/pen/ERmBqK
``
store/index.jsjavascript`
export default new Vuex.Store({
state: {
wizard: {
step: 1
}
},
mutations: {
setWizardStep(state, step) {
state.wizard.step = step;
}
}
});
#### Target selected object fields exposition (action based)
https://codepen.io/anon/pen/eKWqOm
`vue
`
#### Exposed target hook usage
`vue
`
Target mapper
Exposes some properties of target object into computed properties compatible bunch
of getters or/and setters
Targetreturns Target instance with specified path
Targetreturns Target instance with specified state path
* Target
* new Target(path)
* _instance_
* .expose(projection) ⇒ TargetExposition
* .commit(mutation) ⇒ Target
* .dispatch(action) ⇒ Target
* .hook(dispatcher) ⇒ Target
.map(alias) ⇒ \
* .use(plugin) ⇒ Target
* _inner_
* ~dispatcher : function
| Param | Type | Description |
| --- | --- | --- |
| path | string | dot-notation path to some property of your vm instance |
Also, both dispatch() and hook() can provide object mapped by Target instance to the callee, while settingtrue
the second argument (you can read more in the documentation for them)
| Param | Type | Description |
| --- | --- | --- |
| projection | array | target object properties to be exposed |
method.Sets
mutation to be commited on mapped property changemutation shall be called in the format:commit(mutation, newValue)
| Param | Type | Description |
| --- | --- | --- |
| mutation | string | mutation name |
$3
In fact, that's syntax sugar for hook() method.Sets
action to be dispatched on mapped property changeYour
action shall be called in the format:dispatch(action, newValue)
| Param | Type | Description |
| --- | --- | --- |
| action | string | action name |
$3
Set hook that should be run on mapped property change.
| Param | Type |
| --- | --- |
| dispatcher | dispatcher |
$3
returns computed property pair of getters or/and setters for specified projection.If an alias is set, it can be used with spread operator setting provided alias as the computed property name
| Param | Type | Description |
| --- | --- | --- |
| alias | String | name of computed field target to be accessible |
$3
apply pluginplugin is described by object, composed in such format:
`javascript
{
setter: function(key, value, nextSetter) { //setter is mandatory
nextSetter(value);
},
getter: function(key, nextGetter) { //getter is optional
return nextGetter();
},
inject: { // optional, here you can describe additional fields, you want to inject into result map
$internal: {
get() { ... },
set(value) { ... }
}
}
}
`
| Param | Type | Description |
| --- | --- | --- |
| plugin | Object | object, describing your plugin. |
$3
| Param | Type | Description |
| --- | --- | --- |
| store | Store |
vuex store |
| value | mixed | |TargetExposition
Exposes some properties of target object into computed properties compatible bunch
of getters or/and setters
* TargetExposition
* new TargetExposition(target, projection)
* _instance_
* .commit(mutation, sendTarget) ⇒ TargetExposition
* .dispatch(action, sendTarget) ⇒ TargetExposition
* .hook(dispatcher, sendTarget) ⇒ TargetExposition
* .map() ⇒ Object
* .use(plugin) ⇒ TargetExposition
* _inner_
* ~dispatcher : function
$3
| Param | Type |
| --- | --- |
| target | Target |
| projection | Array |
$3
Sets mutation to be commited on exposed field change
if sendTarget is false, action shall be called in format:commit(mutation, { key, value })otherwise, if
sendTarget is set to truecommit(mutation, { target, key, value })Hint: That's just syntax sugar for
hook() method.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| mutation | String | | name of mutation |
| sendTarget | Boolean | false | send target to action |
$3
Sets action to be dispatched on exposed field change.
if sendTarget is false, action shall be called in format:dispatch(action, { key, value })otherwise, if
sendTarget is set to truedispatch(action, { target, key, value })Hint: That's just syntax sugar for
hook() method.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| action | String | | name of action |
| sendTarget | Boolean | false | send target to action |
$3
set callback to be run on property change
| Param | Type |
| --- | --- |
| dispatcher | dispatcher |
| sendTarget | Boolean |
$3
generates map of getters or/and setters for specified projection$3
look Target.use(plugin)
| Param |
| --- |
| plugin |
$3
| Param | Type | Description |
| --- | --- | --- |
| store | Store |
vuex` store |\* | changed value |String | key of changed field |\* | parent object of changed field |Target
| Param | Type | Description |
| --- | --- | --- |
| path | string | dotted path to target property of your component instance |
Target
| Param |
| --- |
| namespace |
| path |