debian-mirror-gitlab/doc/development/fe_guide/droplab/plugins/ajax.md

39 lines
1.1 KiB
Markdown
Raw Normal View History

2017-08-17 22:00:37 +05:30
# Ajax
`Ajax` is a droplab plugin that allows for retrieving and rendering list data from a server.
## Usage
Add the `Ajax` object to the plugins array of a `DropLab.prototype.init` or `DropLab.prototype.addHook` call.
`Ajax` requires 2 config values, the `endpoint` and `method`.
2019-03-02 22:35:43 +05:30
- `endpoint` should be a URL to the request endpoint.
- `method` should be `setData` or `addData`.
- `setData` completely replaces the dropdown with the response data.
- `addData` appends the response data to the current dropdown list.
2017-08-17 22:00:37 +05:30
```html
<a href="#" id="trigger" data-dropdown-trigger="#list">Toggle</a>
<ul id="list" data-dropdown><!-- ... --><ul>
```
2019-09-30 21:07:59 +05:30
2017-08-17 22:00:37 +05:30
```js
2019-09-30 21:07:59 +05:30
const droplab = new DropLab();
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
const trigger = document.getElementById('trigger');
const list = document.getElementById('list');
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
droplab.addHook(trigger, list, [Ajax], {
Ajax: {
endpoint: '/some-endpoint',
method: 'setData',
},
});
2017-08-17 22:00:37 +05:30
```
Optionally you can set `loadingTemplate` to a HTML string. This HTML string will
2020-03-09 13:42:32 +05:30
replace the dropdown list while the request is pending.
2017-08-17 22:00:37 +05:30
Additionally, you can set `onError` to a function to catch any XHR errors.