Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nestedSchema support #67

Merged
merged 1 commit into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/form/fields/field-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ export default {
onCreated() {
if (this.$parent.$isForm) {
this.form = this.$parent;
if (this.form && this.form.model) {
this.model = this.form.model;
}
}
},

Expand Down Expand Up @@ -177,10 +180,6 @@ export default {
if (!this._props.editorWidth.raw && form.editorWidth) {
this.editorWidth = form.editorWidth;
}

if (form && form.model) {
this.model = form.model;
}
}

if (this.model && this.model.$on) {
Expand Down
26 changes: 22 additions & 4 deletions src/schema/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { default as validatorFnMap } from './validators';
import { formatDate, merge, getPath } from '../util';
import { formatDate, merge, getPath, getNestedPath } from '../util';
import { default as defaultMessages } from './messages';

var doValidate = function(object, property, descriptor, rule) {
Expand All @@ -14,8 +14,9 @@ var doValidate = function(object, property, descriptor, rule) {
}

var clonedRule = merge({ message: message || '' }, rule);
let target = getPath(object, property);

if (!validateFn.call(object, object[property], clonedRule, property, descriptor)) {
if (!validateFn.call(object, target, clonedRule, property, descriptor)) {
object.$hints[property] = clonedRule.message;

return false;
Expand Down Expand Up @@ -210,6 +211,12 @@ class Schema {
}
}

for (let nestedSchema in this.nestedSchema) {
if (this.nestedSchema.hasOwnProperty(nestedSchema)) {
result[nestedSchema] = this.nestedSchema[nestedSchema].newModel();
}
}

initObject(result, this, true);

return result;
Expand All @@ -220,7 +227,13 @@ class Schema {
}

getPropertyLabel(property) {
return (this.props[property] || {}).label || '';
let target;
if (property.indexOf('.') > -1) {
target = getNestedPath(this.props, property);
} else {
target = this.props[property];
}
return (target || {}).label || '';
}

getPropertyMapping(property, object, ...args) {
Expand Down Expand Up @@ -334,7 +347,12 @@ class Schema {
initObject(object, this);

const props = this.props;
const descriptor = props[property];
let descriptor;
if (property.indexOf('.') > -1) {
descriptor = getNestedPath(props, property);
} else {
descriptor = props[property];
}

if (!descriptor) {
console.warn(`no property ${property} found in object:`, object); // eslint-disable-line no-console
Expand Down
6 changes: 6 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export function throttle(fn, delay) {
};
}

export function getNestedPath(object, nestedProp) {
let propertyArr = nestedProp.split('.');
let property = propertyArr.pop();
return getPath(object, propertyArr.join('.')).fields[property];
}

export function getPath(object, prop) {
prop = prop || '';
var paths = prop.split('.');
Expand Down