pantagruel74 / yii2-axios-ajax-asset
Axios ajax asset bundle and helpers
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-11-03 11:00:25 UTC
README
Requirements
- PHP >=7.4
- Composer >=2.0
- Yii2 project
- Apache or NGINX server
Files structure
Source code, assets and helpers contains into "src" folder.
All other folders in package need for Acceptance testing of package.
Usage
Connect asset bundle by one of this way
- Register asset-bundle into your view like this:
Yii2AxiosAjaxAssetBundle.register($this);
- Register asset-bundle into your controller like this:
Yii2AxiosAjaxAssetBundle.register($this->getView());
- Add asset bundle as dependency to your main asset bundle
- Register asset-bundle into your view like this:
Use js functions which this bundle allows you:
sendAjax(url, data, method)
- sending ajax with data or params to Url, and return axios promise.- param
url
is target url - param
data
is object of params like{p1: v1, p2: v2}
, or FormData object - param
method
is string shows request method like 'get' or 'post' - return AxiosPromise
- example:
sendAjax('/site/get-form-data', {id: 12}, 'get').then((res) => {if(res.data.html) console.log(res.data.html)});
- param
sendDataAjax(data, formParams, reactionsObj)
- sending ajax with registered response data reaction. Returns void.- param
data
is object of params like{p1: v1, p2: v2}
, or FormData object - param
formParams
is object of form settings like{action: '/some/action', method: 'get'}
- param
reactionsObj
is object registering reaction of response, like{html: (res) => console.log(res.html), error: (res) => {window.alert("Error: " + res.error)}}
- return void
- example:
sendDataAjax({id: 21}, {action: '/site/get-form-data', method: 'get'}, {html: (resData) => console.log(resData.html)})
- param
sendContainerDataAjax(container, formParams = {}, extraParams = {}, reactionsObj = {})
- collects form-elements-data in selected dom-element and send it ajax with registered reaction- param
container
- dom-element contains form-elements to collect and send - param
formParams
is object of form settings like{action: '/some/action', method: 'get'}
- param
extraParams
is object of extra params to add to collected from form-elements data - param
reactionsObj
is object registering reaction of response, like{html: (res) => console.log(res.html), error: (res) => {window.alert("Error: " + res.error)}}
- return void
- example:
<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>
- param
sendFormAjax(form, extraParams = {}, reactionsObj = {})
- collect form-elements-data from selected form and send it ajax with registered reaction- param
form
- form dom element - param
extraParams
is object of extra params to add to collected from form-elements data - param
reactionsObj
is object registering reaction of response, like{html: (res) => console.log(res.html), error: (res) => {window.alert("Error: " + res.error)}}
- return void
- example:
<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>
- param
License
- MIT
Author
- Anatoly Starodubtsev
- Tostar74@mail.ru