Pārlūkot izejas kodu

gridInvokeBuild 可以自定义

luoyifan 3 gadi atpakaļ
vecāks
revīzija
00c9ca1ed8
4 mainītis faili ar 76 papildinājumiem un 29 dzēšanām
  1. 4 27
      src/controls/grid.js
  2. 47 0
      src/controls/stores.js
  3. 1 0
      src/lib/ajax.ts
  4. 24 2
      src/lib/config.ts

+ 4 - 27
src/controls/grid.js

@@ -14,6 +14,7 @@ import {
     YvBase
 } from "../PropertyDescriptionTable";
 import {PropertyDescription} from "../PropertyDescription";
+import {gridInvokeBuild} from "./stores";
 
 export default function () {
 
@@ -199,33 +200,9 @@ export default function () {
             //     this.store.reload({aaaa: 1, bbbb: 2})
             // }
             const {dataSource} = config
-            if (_.isPlainObject(dataSource) && dataSource.method === 'invoke' && !window["IS_DESIGN_MODE"]) {
+            if (_.isPlainObject(dataSource) && !window["IS_DESIGN_MODE"]) {
                 const scope = lookupScope(me)
-
-                const params = calcObjectFlat(scope.viewModel.data, dataSource.params)
-                me.setStore(new Ext.data.Store({
-                    remoteSort: config.remoteSort,
-                    remoteFilter: config.remoteFilter,
-                    autoLoad: true,
-                    proxy: {
-                        type: 'jsonAjax',
-                        $owner: me,
-                        url: serverInvokeUrlTransform(dataSource.url),
-                        extraParams: _.defaultsDeep({}, reloadParams, params),
-                        reader: {
-                            type: 'json',
-                            rootProperty: 'data',
-                            totalProperty: 'pagination.total',
-                            successProperty: 'success',
-                            messageProperty: 'msg'
-                        }
-                    },
-                    listeners: {
-                        load: function (store, records, successful, operation) {
-                            me.fireEvent('dataLoadComplete', me, successful, records);
-                        }
-                    }
-                }))
+                gridInvokeBuild(scope, me, config, dataSource, reloadParams)
             }
         },
 
@@ -249,7 +226,7 @@ export default function () {
                         if (config.dataSourceCallbackFn) {
                             me.reload()
 
-                        } else if (_.isPlainObject(dataSource) && dataSource.method === 'invoke') {
+                        } else if (_.isPlainObject(dataSource)) {
                             me.reload()
                         }
                     }

+ 47 - 0
src/controls/stores.js

@@ -1,5 +1,52 @@
 import _ from 'lodash'
 import {lookupScope} from "../lib/lib";
+import {serverInvokeUrlTransform, apiConvert} from "../lib/config";
+import {calcObjectFlat} from '../lib/systemLib'
+
+/**
+ * 构建一个 grid 支持的 dataSource
+ */
+export function gridInvokeBuild(scope, grid, config, dataSource, reloadParams = {}) {
+    const me = grid
+    const params = calcObjectFlat(scope.viewModel.data, dataSource.params)
+    let storeOption = {}
+
+    if (dataSource.method === 'invoke') {
+        // 默认支持 gridInvoke
+        storeOption = {
+            remoteSort: config.remoteSort,
+            remoteFilter: config.remoteFilter,
+            autoLoad: true,
+            proxy: {
+                type: 'jsonAjax',
+                $owner: me,
+                url: serverInvokeUrlTransform(dataSource.url),
+                extraParams: _.defaultsDeep({}, reloadParams, params),
+                reader: {
+                    type: 'json',
+                    rootProperty: 'data',
+                    totalProperty: 'pagination.total',
+                    successProperty: 'success',
+                    messageProperty: 'msg'
+                }
+            },
+            listeners: {
+                load: function (store, records, successful, operation) {
+                    me.fireEvent('dataLoadComplete', me, successful, records);
+                }
+            }
+        }
+
+    } else if (apiConvert) {
+        // 外部扩展的 apiConvert
+        storeOption = apiConvert.gridInvokeBuild(scope, grid, config, dataSource, params, reloadParams)
+
+    } else {
+        throw new TypeError("不支持的 API 请求方式")
+    }
+
+    me.setStore(new Ext.data.Store(storeOption))
+}
 
 export default function () {
 

+ 1 - 0
src/lib/ajax.ts

@@ -15,6 +15,7 @@ export type ApiMethod =
     | 'post-file'
     | 'invoke'
     | 'sql'
+    | string
 
 export type ApiDataType = 'json' | 'form-data' | 'form'
 

+ 24 - 2
src/lib/config.ts

@@ -6,6 +6,13 @@ export type ServerInvokeUrlTransformFunction = (jsUrl: string) => string | undef
 
 export type SqlUrlTransformFunction = (jsUrl: string) => string | undefined;
 
+export interface ApiConvert {
+    /**
+     * 从表格 dataSource 属性,转换为 ExtJS.storeOption 属性
+     */
+    gridInvokeBuild: (scope, grid, config, dataSource, params, reloadParams) => object
+}
+
 /**
  * YvanUI 全局扩展配置
  */
@@ -22,6 +29,11 @@ export interface ConfigOption {
     ajax: ApiFunction;
 
     /**
+     * 扩展自定义的后端请求方法
+     */
+    apiConvert: ApiConvert;
+
+    /**
      * serverJS Url转换为Ajax请求
      */
     serverInvokeUrlTransform: ServerInvokeUrlTransformFunction;
@@ -39,7 +51,7 @@ export interface ConfigOption {
     /**
      * Scope onload 时扩展方法
      */
-    scopeOnLoad: (scope:any)=> void
+    scopeOnLoad: (scope: any) => void
 }
 
 /**
@@ -54,7 +66,9 @@ export const ajax: {
  */
 let pinyinFunc: Function = (str) => (str)
 
-export let scopeOnLoad : (scope:any)=>void = null
+export let scopeOnLoad: (scope: any) => void = null
+
+export let apiConvert: ApiConvert = undefined
 
 export const baseConfigProcessList: ConfigProcess[] = []
 
@@ -115,11 +129,19 @@ export function extend(option: Partial<ConfigOption>) {
         scopeOnLoad = option.scopeOnLoad
     }
 
+    if (option.apiConvert) {
+        apiConvert = option.apiConvert
+    }
+
     if (typeof option.designMode !== 'undefined') {
         setDesignMode(option.designMode)
     }
 }
 
+export function getApiConvert() {
+    return apiConvert
+}
+
 export function getPinyin(v: string) {
     return pinyinFunc(v)
 }