diff --git a/lib/core/src/ajax/ajax.ts b/lib/core/src/ajax/ajax.ts index f04a2b9153..1c4a2d1347 100644 --- a/lib/core/src/ajax/ajax.ts +++ b/lib/core/src/ajax/ajax.ts @@ -1,6 +1,6 @@ import {$} from '@zui/core'; -import type {AjaxCallbackMap, AjaxCompleteCallback, AjaxErrorCallback, AjaxFormItemValue, AjaxSetting, AjaxSuccessCallback} from './types'; +import type {AjaxBeforeSendCallback, AjaxCallbackMap, AjaxCompleteCallback, AjaxErrorCallback, AjaxFormItemValue, AjaxOptionsModifier, AjaxSetting, AjaxSuccessCallback} from './types'; function setHeader(headers: HeadersInit, name: string, value: string) { if (headers instanceof Headers) { @@ -72,7 +72,9 @@ export function createFormData(data: string | FormData | URLSearchParams | Recor return formData; } -export class Ajax { +export class Ajax { + static globalBeforeSends: AjaxBeforeSendCallback[] = []; + private declare _timeoutID: number; private _controller: AbortController; @@ -199,9 +201,6 @@ export class Ajax { ...initOptions } = this.setting; - if (beforeSend?.(initOptions) === false) { - return; - } if (type) { initOptions.method = type; } @@ -227,6 +226,21 @@ export class Ajax { this.abort(); }); } + + const beforeSends = [...(this.constructor as typeof Ajax).globalBeforeSends, beforeSend]; + for (const callback of beforeSends) { + if (!callback) { + continue; + } + const result = callback.call(this, initOptions); + if (result === false) { + return; + } + if (result) { + Object.assign(initOptions, result); + } + } + if (success) { this.success(success); }