Skip to content

Commit 3e8d1a1

Browse files
committed
fix nested models
1 parent 8636178 commit 3e8d1a1

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/fields/abstractField.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { each, isFunction, isString, isArray, isUndefined } from "lodash";
1+
import Vue from "vue";
2+
import { get as objGet, set as objSet, each, isFunction, isString, isArray, isUndefined } from "lodash";
23

34
export default {
45
props: [
@@ -16,7 +17,7 @@ export default {
1617
val = this.schema.get(this.model);
1718

1819
else if (this.model && this.schema.model)
19-
val = this.model[this.schema.model];
20+
val = objGet(this.model, this.schema.model);
2021

2122
if (isFunction(this.formatValueToField))
2223
val = this.formatValueToField(val);
@@ -35,7 +36,8 @@ export default {
3536
this.$emit("model-updated", this.model[this.schema.model], this.schema.model);
3637

3738
} else if (this.schema.model) {
38-
this.$set(this.model, this.schema.model, newValue);
39+
objSet(this.model, this.schema.model, newValue);
40+
3941
// console.log("model-updated via normal", this.model[this.schema.model]);
4042
this.$emit("model-updated", this.model[this.schema.model], this.schema.model);
4143
}

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

+28
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,34 @@ describe("abstractField.vue", function() {
5555

5656
});
5757

58+
describe("check nested value", () => {
59+
let schema = {
60+
type: "text",
61+
label: "Name",
62+
model: "user.name"
63+
};
64+
let model = {
65+
user: {
66+
name: "John Doe"
67+
}
68+
};
69+
70+
beforeEach( () => {
71+
createField(this, schema, model);
72+
});
73+
74+
it("should give the model static value", () => {
75+
expect(field).to.be.exist;
76+
expect(field.value).to.be.equal("John Doe");
77+
});
78+
79+
it("should set new value to model if value changed", () => {
80+
field.value = "Foo Bar";
81+
expect(model.user.name).to.be.equal("Foo Bar");
82+
});
83+
84+
});
85+
5886
describe("check value as get/set function", () => {
5987
let schema = {
6088
type: "text",

0 commit comments

Comments
 (0)