mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-07 03:43:48 -05:00
See parent commit for the reason we’re doing this. Currently our AJAX requests only work as `GET` requests. So this commit does a bit of work to make them work as `POST` requests. This is optional behaviour, and will only happen when the element in the page that should be updated with AJAX has the `data-form` attribute set. It will take the form that has the corresponding `id`, serialise it, and use that data as the body of the post request. If not form is specified it will not do the serialisation, and submit as a `GET` request as before.
55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
(function(Modules) {
|
|
"use strict";
|
|
|
|
var queues = {};
|
|
var dd = new diffDOM();
|
|
|
|
var getRenderer = $component => response => dd.apply(
|
|
$component.get(0),
|
|
dd.diff($component.get(0), $(response[$component.data('key')]).get(0))
|
|
);
|
|
|
|
var getQueue = resource => (
|
|
queues[resource] = queues[resource] || []
|
|
);
|
|
|
|
var flushQueue = function(queue, response) {
|
|
while(queue.length) queue.shift()(response);
|
|
};
|
|
|
|
var clearQueue = queue => (queue.length = 0);
|
|
|
|
var poll = function(renderer, resource, queue, interval, form) {
|
|
|
|
if (queue.push(renderer) === 1) $.ajax(
|
|
resource,
|
|
{
|
|
'method': form ? 'post' : 'get',
|
|
'data': form ? $('#' + form).serialize() : {}
|
|
}
|
|
).done(
|
|
response => flushQueue(queue, response)
|
|
).fail(
|
|
() => poll = function(){}
|
|
);
|
|
|
|
setTimeout(
|
|
() => poll(...arguments), interval
|
|
);
|
|
|
|
};
|
|
|
|
Modules.UpdateContent = function() {
|
|
|
|
this.start = component => poll(
|
|
getRenderer($(component)),
|
|
$(component).data('resource'),
|
|
getQueue($(component).data('resource')),
|
|
($(component).data('interval-seconds') || 1.5) * 1000,
|
|
$(component).data('form')
|
|
);
|
|
|
|
};
|
|
|
|
})(window.GOVUK.Modules);
|