不管成功还是失败,有数据就该 merge

This commit is contained in:
liaoxuezhi 2019-07-16 16:53:45 +08:00
parent a46397f4e1
commit bc3a8fb0d9
3 changed files with 36 additions and 18 deletions

View File

@ -9,7 +9,7 @@ import {
IRendererStore
} from './index';
import { ServiceStore } from './service';
import { extendObject, createObject, isObjectShallowModified, sortArray } from '../utils/helper';
import { extendObject, createObject, isObjectShallowModified, sortArray, isEmpty } from '../utils/helper';
import {
Api,
Payload,
@ -283,15 +283,18 @@ export const CRUDStore = ServiceStore
const json:Payload = yield (getRoot(self) as IRendererStore).fetcher(api, data, options);
self.markSaving(false);
if (!isEmpty(json.data)) {
self.updateData(json.data, {
__saved: Date.now()
});
self.updatedAt = Date.now();
}
if (!json.ok) {
self.updateMessage(json.msg || options.errorMessage || '保存失败', true);
(getRoot(self) as IRendererStore).notify('error', self.msg);
throw new ServerError(self.msg);
} else {
self.updateData(json.data, {
__saved: Date.now()
});
self.updatedAt = Date.now();
self.updateMessage(json.msg || options.successMessage || '保存成功');
(getRoot(self) as IRendererStore).notify('success', self.msg);
}

View File

@ -26,7 +26,9 @@ import {
cloneObject,
createObject,
difference,
guid
guid,
isObject,
isEmpty
} from '../utils/helper';
import { IComboStore } from "./combo";
import isEqual = require('lodash/isEqual');
@ -204,6 +206,13 @@ export const FormStore = ServiceStore
self.markSaving(true);
const json:Payload = yield (getRoot(self) as IRendererStore).fetcher(api, data, options);
// 失败也同样 merge如果有数据的话。
if (!isEmpty(json.data)) {
setValues(json.data, {
__saved: Date.now()
});
self.updatedAt = Date.now();
}
if (!json.ok) {
// 验证错误
@ -224,13 +233,10 @@ export const FormStore = ServiceStore
self.updateMessage(json.msg || options && options.errorMessage, true);
}
throw new ServerError(self.msg);
} else {
setValues(json.data, {
__saved: Date.now()
});
self.updatedAt = Date.now();
if (options && options.onSuccess) {
const ret = options.onSuccess(json);

View File

@ -153,13 +153,16 @@ export const ServiceStore = iRendererStore
});
fetchCancel = null;
if (!isEmpty(json.data)) {
self.updateData(json.data);
self.updatedAt = Date.now();
self.hasRemoteData = true;
}
if (!json.ok) {
updateMessage(json.msg || options && options.errorMessage, true);
(getRoot(self) as IRendererStore).notify('error', self.msg);
} else {
self.updateData(json.data);
self.updatedAt = Date.now();
self.hasRemoteData = true;
if (options && options.onSuccess) {
const ret = options.onSuccess(json);
@ -209,12 +212,16 @@ export const ServiceStore = iRendererStore
const json:Payload = yield (getRoot(self) as IRendererStore).fetcher(api, data, options);
if (!isEmpty(json.data)) {
self.updateData(json.data);
self.updatedAt = Date.now();
}
if (!json.ok) {
updateMessage(json.msg || options && options.errorMessage || '保存失败', true);
throw new Error(self.msg);
} else {
self.updateData(json.data);
isEmpty(json.data) || (self.updatedAt = Date.now());
if (options && options.onSuccess) {
const ret = options.onSuccess(json);
@ -270,8 +277,10 @@ export const ServiceStore = iRendererStore
const json:Payload = yield (getRoot(self) as IRendererStore).fetcher(api, data, options);
fetchSchemaCancel = null;
self.schema = json.data;
self.schemaKey = '' + Date.now();
if (!isEmpty(json.data)) {
self.schema = json.data;
self.schemaKey = '' + Date.now();
}
if (!json.ok) {
updateMessage(json.msg || options && options.errorMessage || '获取失败,请重试', true);