You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-7Lines changed: 38 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
# vue-deepset
2
-
Deep set Vue.js objects
2
+
Deep set Vue.js objects using dynamic paths
3
3
4
4
---
5
5
6
6
Binding deeply nested data properties and vuex data to a form or component can be tricky. The following set of tools aims to simplify data bindings. Compatible with `Vue 1.x`, `Vue 2.x`, `Vuex 1.x`, and `Vuex 2.x`
7
7
8
-
**Note**`vueModel` and `vuexModel` use [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) objects if supported by the browser and fallback to an object with generated fields based on the target object. Because of this, it is always best to pre-define the properties of an object.
8
+
**Note**`vueModel` and `vuexModel` use [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) objects if supported by the browser and fallback to an object with generated fields based on the target object. Because of this, it is always best to pre-define the properties of an object when using older browsers.
9
9
10
-
Also note that models are flat and once built can set vue/vuex directly using `model[path] = value`
10
+
Also note that models are flat and once built can set vue/vuex directly using `model[path] = value` where path is a lodash formatted path string or path array.
11
11
12
12
### Examples
13
13
@@ -18,9 +18,41 @@ Full examples can be found in the [tests](https://github.com/bhoriuchi/vue-deeps
18
18
*`vue@>=1.0.0`
19
19
*`vuex@>=1.0.0` (optional)
20
20
21
+
### Dynamic paths
22
+
23
+
If you knew what every path you needed ahead of time you could (while tedious) create custom computed properties with getter and setter methods for each of those properties. But what if you have a dynamic and deeply nested property? This problem was actually what inspired the creation of this library. Using a Proxy, `vue-deepset` is able to dynamically create new, deep, reactive properties as well as return `undefined` for values that are not yet set.
24
+
21
25
### Path Strings
22
26
23
-
The modeling methods use `lodash.toPath` format for path strings. Please ensure references use this format
27
+
The modeling methods use `lodash.toPath` format for path strings. Please ensure references use this format. You may also use `path Arrays` which can be easier to construct when using keys that include dots.
28
+
29
+
The following 2 path values are the same
30
+
```js
31
+
conststringPath='a.b["c.d"].e[0]'
32
+
constarrayPath= [ 'a', 'b', 'c.d', 'e', 0 ]
33
+
```
34
+
35
+
#### Keys with dots
36
+
37
+
Since dots prefix a nested path and are also valid characters for a key data that looks like the following can be tricky
38
+
39
+
```js
40
+
constdata= {
41
+
'foo.bar':'baz',
42
+
foo: {
43
+
bar:'qux'
44
+
}
45
+
}
46
+
```
47
+
48
+
So care should be taken when building the path string (or just use an array path) as the following will be true
49
+
50
+
```js
51
+
'foo.bar'// qux
52
+
'["foo.bar"]'// baz
53
+
'foo["bar"]'// qux
54
+
'["foo"].bar'// qux
55
+
```
24
56
25
57
### Binding `v-model` to deeply nested objects
26
58
@@ -35,7 +67,7 @@ Model objects returned by `$deepModel`, `vueModel`, and `vuexModel` are flat and
35
67
## Usage
36
68
37
69
* Webpack `import * as VueDeepSet from 'vue-deepset'`
0 commit comments