|
1 |
| -import { get as objGet, set as objSet, each, isFunction, isString, isArray, isUndefined } from "lodash"; |
| 1 | +import { get as objGet, each, isFunction, isString, isArray, isUndefined } from "lodash"; |
2 | 2 |
|
3 | 3 | export default {
|
4 | 4 | props: [
|
@@ -35,7 +35,7 @@ export default {
|
35 | 35 | this.$emit("model-updated", this.model[this.schema.model], this.schema.model);
|
36 | 36 |
|
37 | 37 | } else if (this.schema.model) {
|
38 |
| - objSet(this.model, this.schema.model, newValue); |
| 38 | + this.setModelValueByPath(this.schema.model, newValue); |
39 | 39 |
|
40 | 40 | // console.log("model-updated via normal", this.model[this.schema.model]);
|
41 | 41 | this.$emit("model-updated", this.model[this.schema.model], this.schema.model);
|
@@ -96,6 +96,38 @@ export default {
|
96 | 96 | this.$set(this.schema, "errors", []); // Be reactive
|
97 | 97 | else
|
98 | 98 | this.schema.errors.splice(0); // Clear
|
| 99 | + }, |
| 100 | + |
| 101 | + setModelValueByPath(path, value) { |
| 102 | + // convert array indexes to properties |
| 103 | + let s = path.replace(/\[(\w+)\]/g, ".$1"); |
| 104 | + |
| 105 | + // strip a leading dot |
| 106 | + s = s.replace(/^\./, ""); |
| 107 | + |
| 108 | + let o = this.model; |
| 109 | + const a = s.split("."); |
| 110 | + let i = 0; |
| 111 | + const n = a.length; |
| 112 | + while (i < n) { |
| 113 | + let k = a[i]; |
| 114 | + if (i < n - 1) |
| 115 | + if (o[k] !== undefined) { |
| 116 | + // Found parent property. Step in |
| 117 | + o = o[k]; |
| 118 | + } else { |
| 119 | + // Create missing property (new level) |
| 120 | + this.$root.$set(o, k, {}); |
| 121 | + o = o[k]; |
| 122 | + } |
| 123 | + else { |
| 124 | + // Set final property value |
| 125 | + this.$root.$set(o, k, value); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + ++i; |
| 130 | + } |
99 | 131 | }
|
100 | 132 | }
|
101 | 133 | };
|
0 commit comments