MDL-64348 javascript: change template loading to also load dependencies

Changed the getTemplate function in templates.js to use the
core_output_load_template_with_dependencies function to load the
requested template and all of the dependencies required to render it.

The dependencies are added to the relevant caches so that when the
template is rendered they aren't re-requested from the server.
This commit is contained in:
Ryan Wyllie 2018-12-11 11:09:32 +08:00
parent fbbed5c603
commit 2b92891a88
2 changed files with 39 additions and 8 deletions

File diff suppressed because one or more lines are too long

View File

@ -98,14 +98,20 @@ define([
var parts = templateName.split('/');
var component = parts.shift();
var name = parts.shift();
var searchKey = this.currentThemeName + '/' + templateName;
var currentTheme = this.currentThemeName;
var searchKey = currentTheme + '/' + templateName;
// First try request variables.
if (searchKey in templatePromises) {
return templatePromises[searchKey];
}
// Check the module template cache.
if (searchKey in templateCache) {
templatePromises[searchKey] = $.Deferred().resolve(templateCache[searchKey]).promise();
return templatePromises[searchKey];
}
// Now try local storage.
var cached = storage.get('core_template/' + searchKey);
@ -117,18 +123,43 @@ define([
// Oh well - load via ajax.
var promises = ajax.call([{
methodname: 'core_output_load_template',
methodname: 'core_output_load_template_with_dependencies',
args: {
component: component,
template: name,
themename: this.currentThemeName
themename: currentTheme
}
}], true, false);
templatePromises[searchKey] = promises[0].then(
function(templateSource) {
templateCache[searchKey] = templateSource;
storage.set('core_template/' + searchKey, templateSource);
function(response) {
var templateSource = null;
response.templates.forEach(function(data) {
// Cache all of the dependent templates because we'll need them to render
// the requested template.
var searchKey = [currentTheme, data.component, data.name].join('/');
templateCache[searchKey] = data.value;
storage.set('core_template/' + searchKey, data.value);
if (data.component == component && data.name == name) {
// This is the template that was requested so remember it to return.
templateSource = data.value;
}
});
if (response.strings.length) {
// If we have strings that the template needs then warm the string cache
// with them now so that we don't need to re-fetch them.
str.cache_strings(response.strings.map(function(data) {
return {
component: data.component,
key: data.name,
value: data.value
};
}));
}
return templateSource;
}
);