From 3d4d4bb34d137dc1131c91e0c4247987b0aa72b1 Mon Sep 17 00:00:00 2001 From: Marconi Moreto Jr Date: Thu, 31 Mar 2016 17:48:19 +0800 Subject: [PATCH] add template caching --- lib/angular-tooltips.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/angular-tooltips.js b/lib/angular-tooltips.js index ba13efc..2e8d9d4 100644 --- a/lib/angular-tooltips.js +++ b/lib/angular-tooltips.js @@ -221,7 +221,7 @@ } }; } - , tooltipDirective = /*@ngInject*/ function tooltipDirective($log, $http, $compile, $timeout, $controller, $injector, tooltipsConf) { + , tooltipDirective = /*@ngInject*/ function tooltipDirective($log, $http, $compile, $timeout, $controller, $injector, $templateCache, tooltipsConf) { var linkingFunction = function linkingFunction($scope, $element, $attrs, $controllerDirective, $transcludeFunc) { @@ -550,22 +550,27 @@ } } , onTooltipTemplateUrlChange = function onTooltipTemplateUrlChange(newValue) { - if (newValue) { - - $http.get(newValue).then(function onResponse(response) { - - if (response && - response.data) { - + var applyTemplate = function applyTemplate(template) { tipTipElement.empty(); tipTipElement.append(closeButtonElement); - tipTipElement.append($compile(response.data)(scope)); + tipTipElement.append($compile(template)(scope)); $timeout(function doLater() { - onTooltipShow(); }); } + , cachedTemplate = $templateCache.get(newValue); + + if (cachedTemplate) { + applyTemplate(cachedTemplate); + return; + } + + $http.get(newValue).then(function onResponse(response) { + if (response && response.data) { + cachedTemplate = $templateCache.put(newValue, response.data); + applyTemplate(cachedTemplate); + } }); } }