Skip to content

Commit

Permalink
fix nested models
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Feb 10, 2017
1 parent 8636178 commit 3e8d1a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/fields/abstractField.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { each, isFunction, isString, isArray, isUndefined } from "lodash";
import Vue from "vue";
import { get as objGet, set as objSet, each, isFunction, isString, isArray, isUndefined } from "lodash";

export default {
props: [
Expand All @@ -16,7 +17,7 @@ export default {
val = this.schema.get(this.model);

else if (this.model && this.schema.model)
val = this.model[this.schema.model];
val = objGet(this.model, this.schema.model);

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

} else if (this.schema.model) {
this.$set(this.model, this.schema.model, newValue);
objSet(this.model, this.schema.model, newValue);

// console.log("model-updated via normal", this.model[this.schema.model]);
this.$emit("model-updated", this.model[this.schema.model], this.schema.model);
}
Expand Down
28 changes: 28 additions & 0 deletions test/unit/specs/fields/abstractField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ describe("abstractField.vue", function() {

});

describe("check nested value", () => {
let schema = {
type: "text",
label: "Name",
model: "user.name"
};
let model = {
user: {
name: "John Doe"
}
};

beforeEach( () => {
createField(this, schema, model);
});

it("should give the model static value", () => {
expect(field).to.be.exist;
expect(field.value).to.be.equal("John Doe");
});

it("should set new value to model if value changed", () => {
field.value = "Foo Bar";
expect(model.user.name).to.be.equal("Foo Bar");
});

});

describe("check value as get/set function", () => {
let schema = {
type: "text",
Expand Down

0 comments on commit 3e8d1a1

Please sign in to comment.