return cachePartialPromises[searchKey];
}
- var cachePartialPromise = this.getTemplate(templateName).then(function(templateSource) {
+ // This promise will not be resolved until all child partials are also resolved and ready.
+ // We create it here to allow us to check for recursive inclusion of templates.
+ cachePartialPromises[searchKey] = $.Deferred();
+
+ this.getTemplate(templateName)
+ .then(function(templateSource) {
var partials = this.scanForPartials(templateSource);
- // Ignore templates that include themselves.
var uniquePartials = partials.filter(function(partialName) {
+ // Check for recursion.
+
+ if (typeof cachePartialPromises[this.currentThemeName + '/' + partialName] !== 'undefined') {
+ // Ignore templates which include their parent.
+ return false;
+ }
+
+ // Ignore templates that include themselves.
return partialName != templateName;
- });
+ }.bind(this));
+
+ // Fetch any partial which has not already been fetched.
var fetchThemAll = uniquePartials.map(function(partialName) {
return this.cachePartials(partialName);
}.bind(this));
- return $.when.apply($, fetchThemAll).then(function() {
- return templateSource;
+ // Resolve the templateName promise when all of the children are resolved.
+ return $.when.apply($, fetchThemAll)
+ .then(function() {
+ return cachePartialPromises[searchKey].resolve(templateSource);
});
- }.bind(this));
-
- cachePartialPromises[searchKey] = cachePartialPromise;
+ }.bind(this))
+ .catch(cachePartialPromises[searchKey].reject);
- return cachePartialPromise;
+ return cachePartialPromises[searchKey];
};
/**