Files
notifications-admin/app/assets/javascripts/updateContent.js
Chris Hill-Scott b31c9fbc0d Make job page poll for updates
This is a first go at having the job page update without refreshing.

The approach I’ve taken is to do all the rendering of HTML on the server side,
rather than use a Javascipt templating engine like mustache. This ensures that
we don’t have to maintain two sets of templates.

So the approach is to split the job page into partials. These partials can then:
- be included in the job page to render the whole page
- be rendered indivudually and then returned as a blob of HTML inside a JSON
  response

Then I’ve added a Javascript module which looks for areas of the page that should
be reloaded. For each area of the page it will poll a URL and re-render that
section of the page when it gets new HTML. It implements some throttling so that
API calls will never happen more frequently than 0.67 times/second.
2016-03-03 14:28:36 +00:00

45 lines
942 B
JavaScript

(function(GOVUK, Modules) {
"use strict";
const interval = 1500; // milliseconds
GOVUK.timeCache = {};
GOVUK.resultCache = {};
let getter = function(resource, render) {
if (
GOVUK.resultCache[resource] &&
(Date.now() < GOVUK.timeCache[resource])
) {
render(GOVUK.resultCache[resource]);
} else {
GOVUK.timeCache[resource] = Date.now() + interval;
$.get(
resource,
response => render(GOVUK.resultCache[resource] = response)
);
}
};
let poller = (resource, key, component) => () => getter(
resource, response => component.html(response[key])
);
Modules.UpdateContent = function() {
this.start = function(component) {
const $component = $(component);
setInterval(
poller($component.data('resource'), $component.data('key'), $component),
interval / 5
);
};
};
})(window.GOVUK, window.GOVUK.Modules);