Skip to content

Commit 2bef826

Browse files
committed
fix unreactive model sets (again)
1 parent d199677 commit 2bef826

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/fields/abstractField.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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";
22

33
export default {
44
props: [
@@ -35,7 +35,7 @@ export default {
3535
this.$emit("model-updated", this.model[this.schema.model], this.schema.model);
3636

3737
} else if (this.schema.model) {
38-
objSet(this.model, this.schema.model, newValue);
38+
this.setModelValueByPath(this.schema.model, newValue);
3939

4040
// console.log("model-updated via normal", this.model[this.schema.model]);
4141
this.$emit("model-updated", this.model[this.schema.model], this.schema.model);
@@ -96,6 +96,38 @@ export default {
9696
this.$set(this.schema, "errors", []); // Be reactive
9797
else
9898
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+
}
99131
}
100132
}
101133
};

test/unit/specs/fields/abstractField.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ describe("abstractField.vue", function() {
8383

8484
});
8585

86+
describe("check nested value if not exists", () => {
87+
let schema = {
88+
type: "text",
89+
label: "Name",
90+
model: "user.name.first"
91+
};
92+
let model = {
93+
user: {
94+
}
95+
};
96+
97+
beforeEach( () => {
98+
createField(this, schema, model);
99+
});
100+
101+
it("should give the model static value", () => {
102+
expect(field).to.be.exist;
103+
expect(field.value).to.be.undefined;
104+
});
105+
106+
it("should set new value to model if value changed", () => {
107+
field.value = "Foo Bar";
108+
expect(model.user.name.first).to.be.equal("Foo Bar");
109+
});
110+
111+
});
112+
86113
describe("check value as get/set function", () => {
87114
let schema = {
88115
type: "text",

0 commit comments

Comments
 (0)