MDL-57009 js: Do not fetch the same template twice
Also clean up promises use of done vs then.
This commit is contained in:
Vendored
+1
-1
@@ -1 +1 @@
|
||||
define(["core/mustache","jquery","core/ajax","core/str","core/notification","core/url","core/config","core/localstorage","core/event","core/yui","core/log"],function(a,b,c,d,e,f,g,h,i,j,k){var l=0,m={},n=function(){this.requiredStrings=[],this.requiredJS=[],this.currentThemeName=""};n.prototype.requiredStrings=null,n.prototype.requiredJS=null,n.prototype.currentThemeName="",n.prototype.getTemplate=function(a,d){var e=b.Deferred(),f=a.split("/"),g=f.shift(),i=f.shift(),j=this.currentThemeName+"/"+a;if(j in m)return e.resolve(m[j]),e.promise();var k=h.get("core_template/"+j);if(k)return e.resolve(k),m[j]=k,e.promise();var l=c.call([{methodname:"core_output_load_template",args:{component:g,template:i,themename:this.currentThemeName}}],d,!1);return l[0].done(function(a){h.set("core_template/"+j,a),m[j]=a,e.resolve(a)}).fail(function(a){e.reject(a)}),e.promise()},n.prototype.partialHelper=function(a){var b="";return this.getTemplate(a,!1).done(function(a){b=a}).fail(e.exception),b},n.prototype.pixHelper=function(b,c,d){var e,g=c.split(","),h="",i="",j="";g.length>0&&(h=g.shift().trim()),g.length>0&&(i=g.shift().trim()),g.length>0&&(j=g.join(",").trim());var k=f.imageUrl(h,i),l={attributes:[{name:"src",value:k},{name:"alt",value:d(j)},{name:"title",value:d(j)},{name:"class",value:"smallicon"}]},n=m[this.currentThemeName+"/core/pix_icon"];return e=a.render(n,l,this.partialHelper.bind(this)),e.trim()},n.prototype.jsHelper=function(a,b,c){return this.requiredJS.push(c(b,a)),""},n.prototype.stringHelper=function(a,b,c){var d=b.split(","),e="",f="",g="";d.length>0&&(e=d.shift().trim()),d.length>0&&(f=d.shift().trim()),d.length>0&&(g=d.join(",").trim()),""!==g&&(g=c(g,a)),0===g.indexOf("{")&&0!==g.indexOf("{{")&&(g=JSON.parse(g));var h=this.requiredStrings.length;return this.requiredStrings.push({key:e,component:f,param:g}),"[[_s"+h+"]]"},n.prototype.quoteHelper=function(a,b,c){var d=c(b.trim(),a);return d=d.replace('"','\\"').replace(/([\{\}]{2,3})/g,"{{=<% %>=}}$1<%={{ }}=%>"),'"'+d+'"'},n.prototype.addHelpers=function(a,b){this.currentThemeName=b,this.requiredStrings=[],this.requiredJS=[],a.uniqid=l++,a.str=function(){return this.stringHelper.bind(this,a)}.bind(this),a.pix=function(){return this.pixHelper.bind(this,a)}.bind(this),a.js=function(){return this.jsHelper.bind(this,a)}.bind(this),a.quote=function(){return this.quoteHelper.bind(this,a)}.bind(this),a.globals={config:g},a.currentTheme=b},n.prototype.getJS=function(a){var b="";return this.requiredJS.length>0&&(b=this.requiredJS.join(";\n")),this.treatStringsInContent(b,a)},n.prototype.treatStringsInContent=function(a,b){var c,d,e,f,g,h,i=/\[\[_s\d+\]\]/;do{for(c="",d=a.search(i);d>-1;){c+=a.substring(0,d),a=a.substr(d),e="",f=4,g=a.substr(f,1);do e+=g,f++,g=a.substr(f,1);while("]"!=g);h=b[parseInt(e,10)],"undefined"==typeof h&&(k.debug("Could not find string for pattern [[_s"+e+"]]."),h=""),c+=h,a=a.substr(6+e.length),d=a.search(i)}a=c+a,d=a.search(i)}while(d>-1);return a},n.prototype.doRender=function(c,e,f){var g=b.Deferred();this.currentThemeName=f;var h=this.getTemplate("core/pix_icon",!0);return h.done(function(){this.addHelpers(e,f);var b="";try{b=a.render(c,e,this.partialHelper.bind(this))}catch(h){g.reject(h)}this.requiredStrings.length>0?d.get_strings(this.requiredStrings).then(function(a){b=this.treatStringsInContent(b,a),g.resolve(b,this.getJS(a))}.bind(this)).fail(g.reject):g.resolve(b.trim(),this.getJS([]))}.bind(this)).fail(g.reject),g.promise()};var o=function(a){if(""!==a.trim()){var c=b("<script>").attr("type","text/javascript").html(a);b("head").append(c)}},p=function(a,c,d,e){var f=b(a);if(f.length){var g=b(c),h=null;e?(h=new j.NodeList(f.children().get()),h.destroy(!0),f.empty(),f.append(g)):(h=new j.NodeList(f.get()),h.destroy(!0),f.replaceWith(g)),o(d),i.notifyFilterContentUpdated(g)}};n.prototype.render=function(a,c,d){var e=b.Deferred();"undefined"==typeof d&&(d=g.theme),this.currentThemeName=d;var f=this.getTemplate(a,!0);return f.done(function(a){var b=this.doRender(a,c,d);b.done(function(a,b){e.resolve(a,b)}).fail(function(a){e.reject(a)})}.bind(this)).fail(function(a){e.reject(a)}),e.promise()};var q=function(a,c,d){var e=b(a);e.length&&(e.prepend(c),o(d),i.notifyFilterContentUpdated(e))},r=function(a,c,d){var e=b(a);e.length&&(e.append(c),o(d),i.notifyFilterContentUpdated(e))};return{render:function(a,b,c){var d=new n;return d.render(a,b,c)},runTemplateJS:o,replaceNodeContents:function(a,b,c){p(a,b,c,!0)},replaceNode:function(a,b,c){p(a,b,c,!1)},prependNodeContents:function(a,b,c){q(a,b,c)},appendNodeContents:function(a,b,c){r(a,b,c)}}});
|
||||
define(["core/mustache","jquery","core/ajax","core/str","core/notification","core/url","core/config","core/localstorage","core/event","core/yui","core/log"],function(a,b,c,d,e,f,g,h,i,j,k){var l=0,m={},n=[],o=function(){this.requiredStrings=[],this.requiredJS=[],this.currentThemeName=""};o.prototype.requiredStrings=null,o.prototype.requiredJS=null,o.prototype.currentThemeName="",o.prototype.getTemplate=function(a,d){var e=a.split("/"),f=e.shift(),g=e.shift(),i=this.currentThemeName+"/"+a;if(i in m)return m[i];var j=h.get("core_template/"+i);if(j)return m[i]=b.Deferred().resolve(j).promise(),m[i];var k=c.call([{methodname:"core_output_load_template",args:{component:f,template:g,themename:this.currentThemeName}}],d,!1);return m[i]=k[0].then(function(a){return h.set("core_template/"+i,a),a}),m[i]},o.prototype.partialHelper=function(a){var b="";return this.getTemplate(a,!1).done(function(a){b=a}).fail(e.exception),b},o.prototype.pixHelper=function(b,c,d){var e,g=c.split(","),h="",i="",j="";g.length>0&&(h=g.shift().trim()),g.length>0&&(i=g.shift().trim()),g.length>0&&(j=g.join(",").trim());var k=f.imageUrl(h,i),l={attributes:[{name:"src",value:k},{name:"alt",value:d(j)},{name:"title",value:d(j)},{name:"class",value:"smallicon"}]},m=n[this.currentThemeName];return e=a.render(m,l,this.partialHelper.bind(this)),e.trim()},o.prototype.jsHelper=function(a,b,c){return this.requiredJS.push(c(b,a)),""},o.prototype.stringHelper=function(a,b,c){var d=b.split(","),e="",f="",g="";d.length>0&&(e=d.shift().trim()),d.length>0&&(f=d.shift().trim()),d.length>0&&(g=d.join(",").trim()),""!==g&&(g=c(g,a)),0===g.indexOf("{")&&0!==g.indexOf("{{")&&(g=JSON.parse(g));var h=this.requiredStrings.length;return this.requiredStrings.push({key:e,component:f,param:g}),"[[_s"+h+"]]"},o.prototype.quoteHelper=function(a,b,c){var d=c(b.trim(),a);return d=d.replace('"','\\"').replace(/([\{\}]{2,3})/g,"{{=<% %>=}}$1<%={{ }}=%>"),'"'+d+'"'},o.prototype.addHelpers=function(a,b){this.currentThemeName=b,this.requiredStrings=[],this.requiredJS=[],a.uniqid=l++,a.str=function(){return this.stringHelper.bind(this,a)}.bind(this),a.pix=function(){return this.pixHelper.bind(this,a)}.bind(this),a.js=function(){return this.jsHelper.bind(this,a)}.bind(this),a.quote=function(){return this.quoteHelper.bind(this,a)}.bind(this),a.globals={config:g},a.currentTheme=b},o.prototype.getJS=function(a){var b="";return this.requiredJS.length>0&&(b=this.requiredJS.join(";\n")),this.treatStringsInContent(b,a)},o.prototype.treatStringsInContent=function(a,b){var c,d,e,f,g,h,i=/\[\[_s\d+\]\]/;do{for(c="",d=a.search(i);d>-1;){c+=a.substring(0,d),a=a.substr(d),e="",f=4,g=a.substr(f,1);do e+=g,f++,g=a.substr(f,1);while("]"!=g);h=b[parseInt(e,10)],"undefined"==typeof h&&(k.debug("Could not find string for pattern [[_s"+e+"]]."),h=""),c+=h,a=a.substr(6+e.length),d=a.search(i)}a=c+a,d=a.search(i)}while(d>-1);return a},o.prototype.doRender=function(c,e,f){return this.currentThemeName=f,this.getTemplate("core/pix_icon",!0).then(function(a){n[this.currentThemeName]=a}.bind(this)).then(function(){this.addHelpers(e,f);var g=a.render(c,e,this.partialHelper.bind(this));return this.requiredStrings.length>0?d.get_strings(this.requiredStrings).then(function(a){return g=this.treatStringsInContent(g,a),b.Deferred().resolve(g,this.getJS(a)).promise()}.bind(this)):b.Deferred().resolve(g.trim(),this.getJS([])).promise()}.bind(this))};var p=function(a){if(""!==a.trim()){var c=b("<script>").attr("type","text/javascript").html(a);b("head").append(c)}},q=function(a,c,d,e){var f=b(a);if(f.length){var g=b(c),h=null;e?(h=new j.NodeList(f.children().get()),h.destroy(!0),f.empty(),f.append(g)):(h=new j.NodeList(f.get()),h.destroy(!0),f.replaceWith(g)),p(d),i.notifyFilterContentUpdated(g)}};o.prototype.render=function(a,b,c){return"undefined"==typeof c&&(c=g.theme),this.currentThemeName=c,this.getTemplate(a,!0).then(function(a){return this.doRender(a,b,c)}.bind(this))};var r=function(a,c,d){var e=b(a);e.length&&(e.prepend(c),p(d),i.notifyFilterContentUpdated(e))},s=function(a,c,d){var e=b(a);e.length&&(e.append(c),p(d),i.notifyFilterContentUpdated(e))};return{render:function(a,b,c){var d=new o;return d.render(a,b,c)},runTemplateJS:p,replaceNodeContents:function(a,b,c){q(a,b,c,!0)},replaceNode:function(a,b,c){q(a,b,c,!1)},prependNodeContents:function(a,b,c){r(a,b,c)},appendNodeContents:function(a,b,c){s(a,b,c)}}});
|
||||
+36
-73
@@ -44,6 +44,9 @@ define(['core/mustache',
|
||||
/** @var {string[]} templateCache - Cache of already loaded templates */
|
||||
var templateCache = {};
|
||||
|
||||
/** @var {string[]} pixTemplateCache - The pix icon template must be cached as a string (once per theme) */
|
||||
var pixTemplateCache = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@@ -77,7 +80,6 @@ define(['core/mustache',
|
||||
* @return {Promise} JQuery promise object resolved when the template has been fetched.
|
||||
*/
|
||||
Renderer.prototype.getTemplate = function(templateName, async) {
|
||||
var deferred = $.Deferred();
|
||||
var parts = templateName.split('/');
|
||||
var component = parts.shift();
|
||||
var name = parts.shift();
|
||||
@@ -86,17 +88,15 @@ define(['core/mustache',
|
||||
|
||||
// First try request variables.
|
||||
if (searchKey in templateCache) {
|
||||
deferred.resolve(templateCache[searchKey]);
|
||||
return deferred.promise();
|
||||
return templateCache[searchKey];
|
||||
}
|
||||
|
||||
// Now try local storage.
|
||||
var cached = storage.get('core_template/' + searchKey);
|
||||
|
||||
if (cached) {
|
||||
deferred.resolve(cached);
|
||||
templateCache[searchKey] = cached;
|
||||
return deferred.promise();
|
||||
templateCache[searchKey] = $.Deferred().resolve(cached).promise();
|
||||
return templateCache[searchKey];
|
||||
}
|
||||
|
||||
// Oh well - load via ajax.
|
||||
@@ -109,18 +109,13 @@ define(['core/mustache',
|
||||
}
|
||||
}], async, false);
|
||||
|
||||
promises[0].done(
|
||||
templateCache[searchKey] = promises[0].then(
|
||||
function(templateSource) {
|
||||
storage.set('core_template/' + searchKey, templateSource);
|
||||
templateCache[searchKey] = templateSource;
|
||||
deferred.resolve(templateSource);
|
||||
}
|
||||
).fail(
|
||||
function(ex) {
|
||||
deferred.reject(ex);
|
||||
return templateSource;
|
||||
}
|
||||
);
|
||||
return deferred.promise();
|
||||
return templateCache[searchKey];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -134,10 +129,11 @@ define(['core/mustache',
|
||||
Renderer.prototype.partialHelper = function(name) {
|
||||
var template = '';
|
||||
|
||||
// Don't use "then" here it will always be async even if the promise is resolved immediately.
|
||||
this.getTemplate(name, false).done(
|
||||
function(source) {
|
||||
template = source;
|
||||
}
|
||||
}
|
||||
).fail(notification.exception);
|
||||
|
||||
return template;
|
||||
@@ -180,7 +176,7 @@ define(['core/mustache',
|
||||
]
|
||||
};
|
||||
// We forced loading of this early, so it will be in the cache.
|
||||
var template = templateCache[this.currentThemeName + '/core/pix_icon'];
|
||||
var template = pixTemplateCache[this.currentThemeName];
|
||||
result = mustache.render(template, templatecontext, this.partialHelper.bind(this));
|
||||
return result.trim();
|
||||
};
|
||||
@@ -392,44 +388,32 @@ define(['core/mustache',
|
||||
* @return {Promise} object
|
||||
*/
|
||||
Renderer.prototype.doRender = function(templateSource, context, themeName) {
|
||||
var deferred = $.Deferred();
|
||||
|
||||
this.currentThemeName = themeName;
|
||||
|
||||
// Make sure we fetch this first.
|
||||
var loadPixTemplate = this.getTemplate('core/pix_icon', true);
|
||||
return this.getTemplate('core/pix_icon', true).then(function(str) {
|
||||
// Make sure we fetch this first.
|
||||
pixTemplateCache[this.currentThemeName] = str;
|
||||
}.bind(this)).then(function() {
|
||||
this.addHelpers(context, themeName);
|
||||
var result = mustache.render(templateSource, context, this.partialHelper.bind(this));
|
||||
|
||||
loadPixTemplate.done(
|
||||
function() {
|
||||
this.addHelpers(context, themeName);
|
||||
var result = '';
|
||||
try {
|
||||
result = mustache.render(templateSource, context, this.partialHelper.bind(this));
|
||||
} catch (ex) {
|
||||
deferred.reject(ex);
|
||||
}
|
||||
if (this.requiredStrings.length > 0) {
|
||||
return str.get_strings(this.requiredStrings).then(function(strings) {
|
||||
|
||||
if (this.requiredStrings.length > 0) {
|
||||
str.get_strings(this.requiredStrings)
|
||||
.then(function(strings) {
|
||||
// Why do we not do another call the render here?
|
||||
//
|
||||
// Because that would expose DOS holes. E.g.
|
||||
// I create an assignment called "{{fish" which
|
||||
// would get inserted in the template in the first pass
|
||||
// and cause the template to die on the second pass (unbalanced).
|
||||
|
||||
// Why do we not do another call the render here?
|
||||
//
|
||||
// Because that would expose DOS holes. E.g.
|
||||
// I create an assignment called "{{fish" which
|
||||
// would get inserted in the template in the first pass
|
||||
// and cause the template to die on the second pass (unbalanced).
|
||||
|
||||
result = this.treatStringsInContent(result, strings);
|
||||
deferred.resolve(result, this.getJS(strings));
|
||||
}.bind(this))
|
||||
.fail(deferred.reject);
|
||||
} else {
|
||||
deferred.resolve(result.trim(), this.getJS([]));
|
||||
}
|
||||
}.bind(this)
|
||||
).fail(deferred.reject);
|
||||
return deferred.promise();
|
||||
result = this.treatStringsInContent(result, strings);
|
||||
return $.Deferred().resolve(result, this.getJS(strings)).promise();
|
||||
}.bind(this));
|
||||
} else {
|
||||
return $.Deferred().resolve(result.trim(), this.getJS([])).promise();
|
||||
}
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -499,8 +483,6 @@ define(['core/mustache',
|
||||
* @return {Promise} JQuery promise object resolved when the template has been rendered.
|
||||
*/
|
||||
Renderer.prototype.render = function(templateName, context, themeName) {
|
||||
var deferred = $.Deferred();
|
||||
|
||||
if (typeof (themeName) === "undefined") {
|
||||
// System context by default.
|
||||
themeName = config.theme;
|
||||
@@ -508,28 +490,9 @@ define(['core/mustache',
|
||||
|
||||
this.currentThemeName = themeName;
|
||||
|
||||
var loadTemplate = this.getTemplate(templateName, true);
|
||||
|
||||
loadTemplate.done(
|
||||
function(templateSource) {
|
||||
var renderPromise = this.doRender(templateSource, context, themeName);
|
||||
|
||||
renderPromise.done(
|
||||
function(result, js) {
|
||||
deferred.resolve(result, js);
|
||||
}
|
||||
).fail(
|
||||
function(ex) {
|
||||
deferred.reject(ex);
|
||||
}
|
||||
);
|
||||
}.bind(this)
|
||||
).fail(
|
||||
function(ex) {
|
||||
deferred.reject(ex);
|
||||
}
|
||||
);
|
||||
return deferred.promise();
|
||||
return this.getTemplate(templateName, true).then( function(templateSource) {
|
||||
return this.doRender(templateSource, context, themeName);
|
||||
}.bind(this) );
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user