feat: 🎸 Rax 出码中添加数据源的 dataHandler 并与数据源引擎的对齐参数

This commit is contained in:
牧毅 2020-08-17 10:46:44 +08:00
parent 8114c6f050
commit 42b9db3f32
4 changed files with 91 additions and 31 deletions

View File

@ -13,6 +13,7 @@ import {
} from '../../../types';
import { generateUnknownType } from '../../../utils/compositeType';
import { parseExpressionConvertThis2Context } from '../../../utils/expressionParser';
import { isContainerSchema } from '../../../utils/schema';
import { RAX_CHUNK_NAME } from './const';
@ -46,8 +47,8 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
content: `
_dataSourceList = this._defineDataSourceList();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceList, this._context);`,
_dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, { runtimeConfig: true });`,
linkAfter: [CLASS_DEFINE_CHUNK_NAME.Start],
});
@ -61,26 +62,33 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
linkAfter: [RAX_CHUNK_NAME.ClassDidMountBegin],
});
const dataSource = isContainerSchema(pre.ir) ? pre.ir.dataSource : null;
const dataSourceItems: DataSourceConfig[] = (dataSource && dataSource.list) || [];
const dataSourceConfig = isContainerSchema(pre.ir) ? pre.ir.dataSource : null;
const dataSourceItems: DataSourceConfig[] = (dataSourceConfig && dataSourceConfig.list) || [];
next.chunks.push({
type: ChunkType.STRING,
fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsPrivateMethod,
// TODO: 下面的定义应该需要调用 @ali/lowcode-datasource-engine 的方法来搞:
content: `
_defineDataSourceList() {
return (function(){
return (${generateUnknownType([
_defineDataSourceConfig() {
const __$$context = this._context;
return (${generateUnknownType(
{
...dataSourceConfig,
list: [
...dataSourceItems.map((item) => ({
...item,
isInit: wrapAsFunction(item.isInit),
options: wrapAsFunction(item.options),
})),
])});
}).call(this._context);
}`,
],
},
{
function: (jsFunc) => parseExpressionConvertThis2Context(jsFunc.value, '__$$context'),
expression: (jsExpr) => parseExpressionConvertThis2Context(jsExpr.value, '__$$context'),
},
)});
}`,
linkAfter: [RAX_CHUNK_NAME.ClassRenderEnd],
});
@ -95,12 +103,12 @@ function wrapAsFunction(value: CompositeValue): CompositeValue {
if (isJSExpression(value) || isJSFunction(value)) {
return {
type: 'JSExpression',
value: `() => (${value.value})`,
value: `function(){ return ((${value.value}))}`,
};
}
return {
type: 'JSExpression',
value: `() => (${generateUnknownType(value)})`,
value: `function(){return((${generateUnknownType(value)}))}`,
};
}

View File

@ -19,8 +19,8 @@ class Home$$Page extends Component {
_context = this._createContext();
_dataSourceList = this._defineDataSourceList();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceList, this._context);
_dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, { runtimeConfig: true });
_utils = this._defineUtils();
@ -74,10 +74,9 @@ class Home$$Page extends Component {
return context;
}
_defineDataSourceList() {
return function () {
return [];
}.call(this._context);
_defineDataSourceConfig() {
const __$$context = this._context;
return { list: [] };
}
_defineUtils() {

View File

@ -38,8 +38,8 @@ class Home$$Page extends Component {
_context = this._createContext();
_dataSourceList = this._defineDataSourceList();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceList, this._context);
_dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, { runtimeConfig: true });
_utils = this._defineUtils();
@ -161,24 +161,65 @@ class Home$$Page extends Component {
return context;
}
_defineDataSourceList() {
return function () {
return [
{ id: 'urlParams', type: 'urlParams', isInit: () => undefined, options: () => undefined },
_defineDataSourceConfig() {
const __$$context = this._context;
return {
list: [
{
id: 'urlParams',
type: 'urlParams',
isInit: function () {
return undefined;
},
options: function () {
return undefined;
},
},
{
id: 'user',
type: 'fetch',
options: () => ({ method: 'GET', uri: 'https://shs.alibaba-inc.com/mock/1458/demo/user' }),
isInit: () => undefined,
options: function () {
return {
method: 'GET',
uri: 'https://shs.alibaba-inc.com/mock/1458/demo/user',
};
},
dataHandler: function (response) {
if (!response.success) {
throw new Error(response.message);
}
return response.data;
},
isInit: function () {
return undefined;
},
},
{
id: 'orders',
type: 'fetch',
options: () => ({ method: 'GET', uri: this.state.user.ordersApiUri }),
isInit: () => undefined,
options: function () {
return {
method: 'GET',
uri: __$$context.state.user.ordersApiUri,
};
},
dataHandler: function (response) {
if (!response.success) {
throw new Error(response.message);
}
return response.data.result;
},
isInit: function () {
return undefined;
},
},
];
}.call(this._context);
],
dataHander: function (dataMap) {
console.info('All datasources loaded:', dataMap);
},
};
}
_defineUtils() {

View File

@ -82,6 +82,10 @@
method: 'GET',
uri: 'https://shs.alibaba-inc.com/mock/1458/demo/user',
},
dataHandler: {
type: 'JSFunction',
value: 'function (response) {\nif (!response.success){\n throw new Error(response.message);\n }\n return response.data;\n}',
},
},
// 示例数据源https://shs.alibaba-inc.com/mock/1458/demo/orders
{
@ -94,8 +98,16 @@
value: 'this.state.user.ordersApiUri',
},
},
dataHandler: {
type: 'JSFunction',
value: 'function (response) {\nif (!response.success){\n throw new Error(response.message);\n }\n return response.data.result;\n}',
},
},
],
dataHander: {
type: 'JSFunction',
value: 'function (dataMap) {\n console.info("All datasources loaded:", dataMap);\n}',
},
},
children: [
{