MDL-63667 javascript: fix failed to pre-fetch the template error

This commit is contained in:
Ryan Wyllie 2018-10-16 11:25:19 +08:00 committed by Damyon Wiese
parent c2f1dbcf49
commit 0015f5b8f1
2 changed files with 18 additions and 13 deletions

File diff suppressed because one or more lines are too long

View file

@ -50,6 +50,9 @@ define(['core/mustache',
/** @var {Promise[]} templatePromises - Cache of already loaded template promises */ /** @var {Promise[]} templatePromises - Cache of already loaded template promises */
var templatePromises = {}; var templatePromises = {};
/** @var {Promise[]} cachePartialPromises - Cache of already loaded template partial promises */
var cachePartialPromises = {};
/** @var {Object} iconSystem - Object extending core/iconsystem */ /** @var {Object} iconSystem - Object extending core/iconsystem */
var iconSystem = {}; var iconSystem = {};
@ -648,24 +651,26 @@ define(['core/mustache',
* @return {Promise} JQuery promise object resolved when all partials are in the cache. * @return {Promise} JQuery promise object resolved when all partials are in the cache.
*/ */
Renderer.prototype.cachePartials = function(templateName) { Renderer.prototype.cachePartials = function(templateName) {
return this.getTemplate(templateName).then(function(templateSource) { var searchKey = this.currentThemeName + '/' + templateName;
var i;
var partials = this.scanForPartials(templateSource);
var fetchThemAll = [];
for (i = 0; i < partials.length; i++) { if (searchKey in cachePartialPromises) {
var searchKey = this.currentThemeName + '/' + partials[i]; return cachePartialPromises[searchKey];
if (searchKey in templatePromises) { }
fetchThemAll.push(templatePromises[searchKey]);
} else { var cachePartialPromise = this.getTemplate(templateName).then(function(templateSource) {
fetchThemAll.push(this.cachePartials(partials[i])); var partials = this.scanForPartials(templateSource);
} var fetchThemAll = partials.map(function(partialName) {
} return this.cachePartials(partialName);
}.bind(this));
return $.when.apply($, fetchThemAll).then(function() { return $.when.apply($, fetchThemAll).then(function() {
return templateSource; return templateSource;
}); });
}.bind(this)); }.bind(this));
cachePartialPromises[searchKey] = cachePartialPromise;
return cachePartialPromise;
}; };
/** /**