panthagruel74 / yii2-axios-ajax-asset
Axios ajax 资产包和助手
1.2
2023-08-17 19:00 UTC
Requires
- php: >=7.4.0
- ext-json: *
- yiisoft/yii2: ~2.0.45
Requires (Dev)
- mnemesong/microwidget: *
- pantagruel74/yii2-strictly: *
- phpstan/phpstan: *
- phpunit/phpunit: ~9.5.0
- ramsey/uuid: *
- yiisoft/yii2-debug: ~2.1.0
- yiisoft/yii2-gii: ~2.2.0
This package is not auto-updated.
Last update: 2024-09-22 10:29:16 UTC
README
需求
- PHP >=7.4
- Composer >=2.0
- Yii2 项目
- Apache 或 NGINX 服务器
文件结构
源代码、资产和助手包含在 "src" 文件夹中。
包中的所有其他文件夹都需要进行包的验收测试。
使用方法
通过以下任一方式连接资产包:
- 将资产包注册到您的视图中,如下所示:
Yii2AxiosAjaxAssetBundle.register($this);
- 将资产包注册到您的控制器中,如下所示:
Yii2AxiosAjaxAssetBundle.register($this->getView());
- 将资产包作为依赖项添加到您的主资产包中
- 将资产包注册到您的视图中,如下所示:
使用该包允许的 js 函数
sendAjax(url, data, method)
- 向 URL 发送带有数据或参数的 Ajax 请求,并返回 axios 承诺。- 参数
url
是目标 URL - 参数
data
是类似于{p1: v1, p2: v2}
的参数对象,或 FormData 对象 - 参数
method
是表示请求方法的字符串,如 'get' 或 'post' - 返回 AxiosPromise
- 示例:
sendAjax('/site/get-form-data', {id: 12}, 'get').then((res) => {if(res.data.html) console.log(res.data.html)});
- 参数
sendDataAjax(data, formParams, reactionsObj)
- 带有已注册的响应数据反应发送 Ajax。返回 void。- 参数
data
是类似于{p1: v1, p2: v2}
的参数对象,或 FormData 对象 - 参数
formParams
是类似于{action: '/some/action', method: 'get'}
的表单设置对象 - 参数
reactionsObj
是注册响应反应的对象,如{html: (res) => console.log(res.html), error: (res) => {window.alert("Error: " + res.error)}}
- 返回 void
- 示例:
sendDataAjax({id: 21}, {action: '/site/get-form-data', method: 'get'}, {html: (resData) => console.log(resData.html)})
- 参数
sendContainerDataAjax(container, formParams = {}, extraParams = {}, reactionsObj = {})
- 从选定的 dom 元素中收集表单元素数据,并带有已注册的反应发送 Ajax- 参数
container
- 包含要收集和发送的表单元素的 dom 元素 - 参数
formParams
是类似于{action: '/some/action', method: 'get'}
的表单设置对象 - 参数
extraParams
是要添加到收集的表单元素数据的额外参数对象 - 参数
reactionsObj
是注册响应反应的对象,如{html: (res) => console.log(res.html), error: (res) => {window.alert("Error: " + res.error)}}
- 返回 void
- 示例:
<div><input type="text" onchange="sendContainerDataAjax(this.closest('div'), {action: '/site/get-form-data', method: 'get'}, {id: 12}, {html: (resData) => console.log(resData.html)})"></div>
- 参数
sendFormAjax(form, extraParams = {}, reactionsObj = {})
- 从选定的表单中收集表单元素数据,并带有已注册的反应发送 Ajax- 参数
form
- 表单 dom 元素 - 参数
extraParams
是要添加到收集的表单元素数据的额外参数对象 - 参数
reactionsObj
是注册响应反应的对象,如{html: (res) => console.log(res.html), error: (res) => {window.alert("Error: " + res.error)}}
- 返回 void
- 示例:
<form action="/site/get-form-data" method="get"><input type="text" onchange="sendContainerDataAjax(this.closest('form'), {id: 12}, {html: (resData) => console.log(resData.html)})"></form>
- 参数
许可证
- MIT
作者
- Anatoly Starodubtsev
- Tostar74@mail.ru