api增加replaceData
This commit is contained in:
parent
87d033b119
commit
776c473054
|
@ -2,8 +2,8 @@ import React from "react";
|
|||
import { FormItem, FormControlProps } from "./Item";
|
||||
import cx from "classnames";
|
||||
import Button from "../../components/Button";
|
||||
import { createObject, isObjectShallowModified } from "../../utils/helper";
|
||||
import { RendererData, Action, Api, Payload } from "../../types";
|
||||
import {createObject, isObject, isObjectShallowModified} from '../../utils/helper';
|
||||
import {RendererData, Action, Api, Payload, ApiObject} from '../../types';
|
||||
import { isEffectiveApi } from "../../utils/api";
|
||||
import { filter } from "../../utils/tpl";
|
||||
import omit = require("lodash/omit");
|
||||
|
@ -253,7 +253,7 @@ export default class FormTable extends React.Component<TableProps, TableState> {
|
|||
return;
|
||||
} else if (remote && remote.ok) {
|
||||
item = {
|
||||
...item,
|
||||
...(isObject(updateApi) && (updateApi as ApiObject).replaceData ? {} : item),
|
||||
...remote.data
|
||||
};
|
||||
}
|
||||
|
|
|
@ -3,10 +3,11 @@ import {Renderer, RendererProps} from '../factory';
|
|||
import {ServiceStore, IServiceStore} from '../store/service';
|
||||
import cx from 'classnames';
|
||||
import getExprProperties from '../utils/filter-schema';
|
||||
import {Api, Payload} from '../types';
|
||||
import {Api, ApiObject, Payload} from '../types';
|
||||
import update = require('react-addons-update');
|
||||
import {isEffectiveApi, isApiOutdated} from '../utils/api';
|
||||
import {ScopedContext, IScopedContext} from '../Scoped';
|
||||
import {isObject} from '../utils/helper';
|
||||
|
||||
export interface TaskProps extends RendererProps {
|
||||
className?: string;
|
||||
|
@ -210,8 +211,9 @@ export default class Task extends React.Component<TaskProps, TaskState> {
|
|||
if (Array.isArray(ret.data)) {
|
||||
this.handleLoaded(ret);
|
||||
} else {
|
||||
let replace = isObject(api) && (api as ApiObject).replaceData;
|
||||
const items = this.state.items.map(item =>
|
||||
item.key === ret.data.key
|
||||
(item.key === ret.data.key && !replace)
|
||||
? {
|
||||
...item,
|
||||
...ret.data
|
||||
|
|
|
@ -7,9 +7,10 @@ import {
|
|||
isObjectShallowModified,
|
||||
sortArray,
|
||||
isEmpty,
|
||||
qsstringify
|
||||
qsstringify,
|
||||
isObject
|
||||
} from '../utils/helper';
|
||||
import {Api, Payload, fetchOptions, Action} from '../types';
|
||||
import {Api, Payload, fetchOptions, Action, ApiObject} from '../types';
|
||||
import qs from 'qs';
|
||||
import pick = require('lodash/pick');
|
||||
import {resolveVariableAndFilter} from '../utils/tpl-builtin';
|
||||
|
@ -116,7 +117,7 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
|
|||
syncResponse2Query?: boolean;
|
||||
}
|
||||
) => Promise<any> = flow(function* getInitData(
|
||||
api: string,
|
||||
api: Api,
|
||||
data: object,
|
||||
options: fetchOptions & {
|
||||
forceReload?: boolean;
|
||||
|
@ -263,7 +264,7 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
|
|||
}
|
||||
|
||||
const data = {
|
||||
...self.pristine,
|
||||
...(isObject(api) && (api as ApiObject).replaceData ? {} : self.pristine),
|
||||
items: rowsData,
|
||||
count: count,
|
||||
total: total,
|
||||
|
@ -286,7 +287,7 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
|
|||
}
|
||||
|
||||
self.items.replace(rowsData);
|
||||
self.reInitData(data);
|
||||
self.reInitData(data, isObject(api) && (api as ApiObject).replaceData);
|
||||
options.syncResponse2Query !== false &&
|
||||
updateQuery(
|
||||
pick(rest, Object.keys(self.query)),
|
||||
|
@ -348,7 +349,7 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
|
|||
data?: object,
|
||||
options?: fetchOptions
|
||||
) => Promise<any> = flow(function* saveRemote(
|
||||
api: string,
|
||||
api: Api,
|
||||
data: object,
|
||||
options: fetchOptions = {}
|
||||
) {
|
||||
|
@ -369,7 +370,7 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
|
|||
if (!isEmpty(json.data) || json.ok) {
|
||||
self.updateData(json.data, {
|
||||
__saved: Date.now()
|
||||
});
|
||||
}, isObject(api) && (api as ApiObject).replaceData);
|
||||
self.updatedAt = Date.now();
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ import {types, getEnv, flow, getRoot, detach} from 'mobx-state-tree';
|
|||
import debounce = require('lodash/debounce');
|
||||
import {ServiceStore} from './service';
|
||||
import {FormItemStore, IFormItemStore, SFormItemStore} from './formItem';
|
||||
import {Api, fetchOptions, Payload} from '../types';
|
||||
import {Api, ApiObject, fetchOptions, Payload} from '../types';
|
||||
import {ServerError} from '../utils/errors';
|
||||
import {
|
||||
getVariable,
|
||||
|
@ -81,8 +81,8 @@ export const FormStore = ServiceStore.named('FormStore')
|
|||
}
|
||||
}))
|
||||
.actions(self => {
|
||||
function setValues(values: object, tag?: object) {
|
||||
self.updateData(values, tag);
|
||||
function setValues(values: object, tag?: object, replace?: boolean) {
|
||||
self.updateData(values, tag, replace);
|
||||
|
||||
// 同步 options
|
||||
syncOptions();
|
||||
|
@ -198,7 +198,7 @@ export const FormStore = ServiceStore.named('FormStore')
|
|||
data?: object,
|
||||
options?: fetchOptions
|
||||
) => Promise<any> = flow(function* saveRemote(
|
||||
api: string,
|
||||
api: Api,
|
||||
data: object,
|
||||
options: fetchOptions = {}
|
||||
) {
|
||||
|
@ -231,7 +231,7 @@ export const FormStore = ServiceStore.named('FormStore')
|
|||
if (!isEmpty(json.data) || json.ok) {
|
||||
setValues(json.data, {
|
||||
__saved: Date.now()
|
||||
});
|
||||
}, isObject(api) && (api as ApiObject).replaceData);
|
||||
self.updatedAt = Date.now();
|
||||
}
|
||||
|
||||
|
|
|
@ -62,17 +62,17 @@ export const iRendererStore = types
|
|||
self.data = self.pristine;
|
||||
},
|
||||
|
||||
updateData(data: object = {}, tag?: object) {
|
||||
updateData(data: object = {}, tag?: object, replace?:boolean) {
|
||||
const prev = self.data;
|
||||
let newData;
|
||||
if (tag) {
|
||||
let proto = createObject((self.data as any).__super || null, tag);
|
||||
let proto = createObject(!replace && (self.data as any).__super || null, tag);
|
||||
newData = createObject(proto, {
|
||||
...self.data,
|
||||
...(replace ? {} : self.data),
|
||||
...data
|
||||
});
|
||||
} else {
|
||||
newData = extendObject(self.data, data);
|
||||
newData = replace ? data : extendObject(self.data, data);
|
||||
}
|
||||
|
||||
Object.defineProperty(newData, '__prev', {
|
||||
|
|
|
@ -39,8 +39,8 @@ export const ServiceStore = iRendererStore
|
|||
self.busying = busying;
|
||||
}
|
||||
|
||||
function reInitData(data: object | undefined) {
|
||||
const newData = extendObject(self.pristine, data);
|
||||
function reInitData(data: object | undefined, replace: boolean = false) {
|
||||
const newData = replace ? data : extendObject(self.pristine, data);
|
||||
self.data = self.pristine = newData;
|
||||
}
|
||||
|
||||
|
@ -58,7 +58,7 @@ export const ServiceStore = iRendererStore
|
|||
data?: object,
|
||||
options?: fetchOptions
|
||||
) => Promise<any> = flow(function* getInitData(
|
||||
api: string,
|
||||
api: Api,
|
||||
data: object,
|
||||
options?: fetchOptions
|
||||
) {
|
||||
|
@ -97,10 +97,12 @@ export const ServiceStore = iRendererStore
|
|||
: undefined
|
||||
);
|
||||
} else {
|
||||
reInitData({
|
||||
...self.data,
|
||||
let replaceData = isObject(api) && (api as ApiObject).replaceData;
|
||||
let data = {
|
||||
...(replaceData ? {} : self.data),
|
||||
...json.data
|
||||
});
|
||||
};
|
||||
reInitData(data, replaceData);
|
||||
self.updatedAt = Date.now();
|
||||
self.hasRemoteData = true;
|
||||
if (options && options.onSuccess) {
|
||||
|
@ -143,7 +145,7 @@ export const ServiceStore = iRendererStore
|
|||
data?: object,
|
||||
options?: fetchOptions
|
||||
) => Promise<any> = flow(function* getInitData(
|
||||
api: string,
|
||||
api: Api,
|
||||
data: object,
|
||||
options?: fetchOptions
|
||||
) {
|
||||
|
@ -168,7 +170,7 @@ export const ServiceStore = iRendererStore
|
|||
fetchCancel = null;
|
||||
|
||||
if (!isEmpty(json.data) || json.ok) {
|
||||
json.data && self.updateData(json.data);
|
||||
json.data && self.updateData(json.data, undefined, isObject(api) && (api as ApiObject).replaceData);
|
||||
self.updatedAt = Date.now();
|
||||
self.hasRemoteData = true;
|
||||
}
|
||||
|
@ -226,7 +228,7 @@ export const ServiceStore = iRendererStore
|
|||
data?: object,
|
||||
options?: fetchOptions
|
||||
) => Promise<any> = flow(function* saveRemote(
|
||||
api: string,
|
||||
api: Api,
|
||||
data: object,
|
||||
options: fetchOptions = {}
|
||||
) {
|
||||
|
@ -248,7 +250,7 @@ export const ServiceStore = iRendererStore
|
|||
);
|
||||
|
||||
if (!isEmpty(json.data) || json.ok) {
|
||||
json.data && self.updateData(json.data);
|
||||
json.data && self.updateData(json.data, undefined, isObject(api) && (api as ApiObject).replaceData);
|
||||
self.updatedAt = Date.now();
|
||||
}
|
||||
|
||||
|
@ -302,7 +304,7 @@ export const ServiceStore = iRendererStore
|
|||
data?: object,
|
||||
options?: fetchOptions
|
||||
) => Promise<any> = flow(function* fetchSchema(
|
||||
api: string,
|
||||
api: Api,
|
||||
data: object,
|
||||
options: fetchOptions = {}
|
||||
) {
|
||||
|
@ -363,7 +365,7 @@ export const ServiceStore = iRendererStore
|
|||
if (json.data) {
|
||||
self.schema = json.data;
|
||||
self.schemaKey = '' + Date.now();
|
||||
isObject(json.data.data) && self.updateData(json.data.data);
|
||||
isObject(json.data.data) && self.updateData(json.data.data, undefined, isObject(api) && (api as ApiObject).replaceData);
|
||||
}
|
||||
updateMessage(json.msg || (options && options.successMessage));
|
||||
|
||||
|
@ -397,7 +399,7 @@ export const ServiceStore = iRendererStore
|
|||
data?: object,
|
||||
options?: fetchOptions
|
||||
) => Promise<any> = flow(function* checkRemote(
|
||||
api: string,
|
||||
api: Api,
|
||||
data: object,
|
||||
options?: fetchOptions
|
||||
) {
|
||||
|
@ -412,7 +414,7 @@ export const ServiceStore = iRendererStore
|
|||
data,
|
||||
options
|
||||
);
|
||||
json.ok && self.updateData(json.data);
|
||||
json.ok && self.updateData(json.data, undefined, isObject(api) && (api as ApiObject).replaceData);
|
||||
|
||||
if (!json.ok) {
|
||||
throw new Error(json.msg);
|
||||
|
|
|
@ -14,6 +14,7 @@ export interface ApiObject {
|
|||
cache?: number;
|
||||
qsOptions?: any;
|
||||
dataType?: 'json' | 'form-data' | 'form';
|
||||
replaceData?: boolean;
|
||||
}
|
||||
export type ApiString = string;
|
||||
export type Api = ApiString | ApiObject;
|
||||
|
|
Loading…
Reference in New Issue