2017-04-07 09:57:03 -04:00
|
|
|
/* eslint-disable */
|
|
|
|
|
2017-05-08 08:35:29 -04:00
|
|
|
import AjaxCache from '~/lib/utils/ajax_cache';
|
|
|
|
|
2017-04-07 09:57:03 -04:00
|
|
|
const Ajax = {
|
|
|
|
_loadData: function _loadData(data, config, self) {
|
|
|
|
if (config.loadingTemplate) {
|
|
|
|
var dataLoadingTemplate = self.hook.list.list.querySelector('[data-loading-template]');
|
|
|
|
if (dataLoadingTemplate) dataLoadingTemplate.outerHTML = self.listTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!self.destroyed) self.hook.list[config.method].call(self.hook.list, data);
|
|
|
|
},
|
2017-08-02 05:27:24 -04:00
|
|
|
preprocessing: function preprocessing(config, data) {
|
|
|
|
let results = data;
|
|
|
|
|
|
|
|
if (config.preprocessing && !data.preprocessed) {
|
|
|
|
results = config.preprocessing(data);
|
|
|
|
AjaxCache.override(config.endpoint, results);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
},
|
2017-04-07 09:57:03 -04:00
|
|
|
init: function init(hook) {
|
|
|
|
var self = this;
|
|
|
|
self.destroyed = false;
|
|
|
|
var config = hook.config.Ajax;
|
|
|
|
this.hook = hook;
|
|
|
|
if (!config || !config.endpoint || !config.method) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (config.method !== 'setData' && config.method !== 'addData') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (config.loadingTemplate) {
|
|
|
|
var dynamicList = hook.list.list.querySelector('[data-dynamic]');
|
|
|
|
var loadingTemplate = document.createElement('div');
|
|
|
|
loadingTemplate.innerHTML = config.loadingTemplate;
|
|
|
|
loadingTemplate.setAttribute('data-loading-template', '');
|
|
|
|
this.listTemplate = dynamicList.outerHTML;
|
|
|
|
dynamicList.outerHTML = loadingTemplate.outerHTML;
|
|
|
|
}
|
2017-05-08 08:35:29 -04:00
|
|
|
|
2017-08-02 05:27:24 -04:00
|
|
|
return AjaxCache.retrieve(config.endpoint)
|
|
|
|
.then(self.preprocessing.bind(null, config))
|
2018-10-10 02:15:56 -04:00
|
|
|
.then(data => self._loadData(data, config, self))
|
2017-05-08 08:35:29 -04:00
|
|
|
.catch(config.onError);
|
2017-04-07 09:57:03 -04:00
|
|
|
},
|
|
|
|
destroy: function() {
|
|
|
|
this.destroyed = true;
|
2018-10-10 02:15:56 -04:00
|
|
|
},
|
2017-04-07 09:57:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Ajax;
|