From 0f03f570340fc60c127e7cf5980eecf13c9749c0 Mon Sep 17 00:00:00 2001 From: Martin Geubelle Date: Wed, 25 Jul 2018 12:03:59 +0200 Subject: [FIX] web: look into the DOM for loadCSS Since rev. odoo/odoo@0dd6a0b loadCSS function in ajax returns a promise in order to known when the stylesheet is loaded. It uses a local cache to avoid fetching the file multiple times. However, the lazy loaded lib could already be in the DOM (in the test bundle for example). In this case, the file was fetched twice. In this case, the cache is now filled with a resolved promise. --- addons/web/static/src/js/core/ajax.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/core/ajax.js b/addons/web/static/src/js/core/ajax.js index 35869b1949d..d1bdd8d9d8d 100644 --- a/addons/web/static/src/js/core/ajax.js +++ b/addons/web/static/src/js/core/ajax.js @@ -197,7 +197,8 @@ function realSetTimeout (fct, millis) { /** * Load css asynchronously: fetch it from the url parameter and add a link tag * to . - * If the url has already been requested and loaded, the promise will resolve immediately. + * If the url has already been requested and loaded, the promise will resolve + * immediately. * * @param {String} url of the css to be fetched * @returns {Deferred} resolved when the css has been loaded. @@ -207,18 +208,22 @@ var loadCSS = (function () { return function loadCSS(url) { if (url in urlDefs) { - return urlDefs[url]; + // nothing to do here + } else if ($('link[href="' + url + '"]').length) { + // the link is already in the DOM, the deferred can be resolved + urlDefs[url] = $.when(); + } else { + var $link = $('', { + 'href': url, + 'rel': 'stylesheet', + 'type': 'text/css' + }); + urlDefs[url] = $.Deferred(); + $link.on('load', function () { + urlDefs[url].resolve(); + }); + $('head').append($link); } - var $link = $('', { - 'href': url, - 'rel': 'stylesheet', - 'type': 'text/css' - }); - urlDefs[url] = $.Deferred(); - $link.on('load', function () { - urlDefs[url].resolve(); - }); - $('head').append($link); return urlDefs[url]; }; })(); -- cgit v1.2.3