From 8609cc1675df00724472eb2111c28292b172d251 Mon Sep 17 00:00:00 2001 From: rabuzarus <> Date: Thu, 5 May 2016 23:48:52 +0200 Subject: [PATCH] hovercard: cache raw content of the template so we don't have to request it everytime from the server --- js/hovercard.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/js/hovercard.js b/js/hovercard.js index 1ca07ffabf..0fccebc34b 100644 --- a/js/hovercard.js +++ b/js/hovercard.js @@ -85,7 +85,6 @@ $(document).ready(function(){ }); - // hover cards should be removed very easily, e.g. when any of these events happen $('body').on("mouseleave touchstart scroll click dblclick mousedown mouseup submit keydown keypress keyup", function(e){ var timeNow = new Date().getTime(); @@ -196,6 +195,7 @@ function getHoverCardContent(term, url, callback) { // }); } + // Ajax request to get the raw template content function getHoverCardTemplate (url, callback) { var postdata = { @@ -203,14 +203,24 @@ function getHoverCardTemplate (url, callback) { datatype: 'tpl' }; + // Look if we have the template already in the cace, so we don't have + // request it again + if('hovercard' in getHoverCardTemplate.cache) { + setTimeout(function() { callback(getHoverCardTemplate.cache['hovercard']); } , 1); + return; + } + $.ajax({ url: url, data: postdata, success: function(data, textStatus) { + // write the data in the cache + getHoverCardTemplate.cache['hovercard'] = data; callback(data); } - }); + }).fail(function () {callback([]); }); } +getHoverCardTemplate.cache = {}; // The Variables used for the template function getHoverCardVariables(object) { -- 2.39.5