summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Geubelle <mge@odoo.com>2018-07-25 12:03:59 +0200
committerMartin Geubelle <mge@odoo.com>2018-07-25 15:08:52 +0200
commit0f03f570340fc60c127e7cf5980eecf13c9749c0 (patch)
treea20468de9521519db0130f7f93c8fe1802b0c579
parent81287c0a25063f2fc74046a475c1b2171151a348 (diff)
[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.
-rw-r--r--addons/web/static/src/js/core/ajax.js29
1 files changed, 17 insertions, 12 deletions
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 <head>.
- * 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 = $('<link>', {
+ 'href': url,
+ 'rel': 'stylesheet',
+ 'type': 'text/css'
+ });
+ urlDefs[url] = $.Deferred();
+ $link.on('load', function () {
+ urlDefs[url].resolve();
+ });
+ $('head').append($link);
}
- var $link = $('<link>', {
- 'href': url,
- 'rel': 'stylesheet',
- 'type': 'text/css'
- });
- urlDefs[url] = $.Deferred();
- $link.on('load', function () {
- urlDefs[url].resolve();
- });
- $('head').append($link);
return urlDefs[url];
};
})();