@caxa-form/core
Version:
vue动态表单,助你轻松搞定表单|form-create is a form generation component that can generate dynamic rendering, data collection, verification and submission functions through JSON. Supports 3 UI frameworks, and supports the generation of any Vue components. Built-in 20
75 lines (67 loc) • 1.68 kB
JavaScript
import {err} from '@caxa-form/utils/lib/console';
import fetch from './fetch';
import {invoke} from './util';
import is from '@caxa-form/utils/lib/type';
import deepSet from '@caxa-form/utils/lib/deepset';
const $fetch = {
name: 'fetch',
loaded(...args) {
run(...args);
},
watch(inject, rule, api) {
if (!run(inject, rule, api)) {
inject.clearProp();
api.sync(rule);
}
}
};
function parseOpt(option) {
if (is.String(option)) {
option = {
action: option,
to: 'options'
}
}
return option;
}
function run(inject, rule, api) {
let option = inject.value;
option = parseOpt(option);
if (!option || !option.action) {
return false;
}
if (!option.to) {
option.to = 'options';
}
const onError = option.onError;
const check = () => {
if (!inject.getValue()) {
inject.clearProp();
api.sync(rule);
return true;
}
}
const set = (val) => {
if (val === undefined) {
inject.clearProp();
api.sync(rule);
} else {
deepSet(inject.getProp(), option.to, val);
}
}
invoke(() => fetch({
...option,
onSuccess(body) {
if (check()) return;
set((option.parse || ((v) => v.data))(body))
api.sync(rule);
},
onError(e) {
set(undefined)
if (check()) return;
(onError || ((e) => err(e.message || 'fetch fail ' + option.action)))(e, rule, api);
}
}));
return true;
}
export default $fetch;