Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit f8d4aa6

Browse files
committed
Fix template caching.
1 parent 44da315 commit f8d4aa6

File tree

7 files changed

+143
-100
lines changed

7 files changed

+143
-100
lines changed

Readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ tooltip-template="" | String() | '' | Set your tooltip template (HTML or just Te
8686
| | | **to know**: don't use it together with `tooltip-template-url` attribute, use only one of them
8787
tooltip-template-url="" | String() | '' | Set your external tooltip template PATH
8888
| | | **to know**: don't use it together with `tooltip-template` attribute, use only one of them
89-
tooltip-template-url-cache="" | String(Boolean) | false | This attribute stores and retrieves the template from the cache
89+
tooltip-template-url-cache="" | String(Boolean) | true | This attribute stores and retrieves the template from the cache
9090
tooltip-controller="" | String() | '' | Set a controller to your external tooltip template
9191
tooltip-smart="" | String(Boolean) | false | Set the tooltip to automatically search the best position on the screen
9292
tooltip-show-trigger="" | String('event1 event2') | 'mouseover' | Show the tooltip on specific event/events
@@ -106,10 +106,10 @@ Sometimes you may need to set all of your tooltips options in one place, you can
106106
```javascript
107107
.config(['tooltipsConfProvider', function configConf(tooltipsConfProvider) {
108108
tooltipsConfProvider.configure({
109-
'smart':true,
110-
'size':'large',
109+
'smart': true,
110+
'size': 'large',
111111
'speed': 'slow',
112-
'tooltipTemplateUrlCache': true
112+
'tooltipTemplateUrlCache': false
113113
//etc...
114114
});
115115
}])

demo/views/template-url-cache.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>I'm a tooltip loaded using templateUrlCache!</div>

dist/angular-tooltips.js

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* http://720kb.github.io/angular-tooltips
77
*
88
* MIT license
9-
* Thu May 18 2017
9+
* Fri May 19 2017
1010
*/
1111
/*global angular,window*/
1212
(function withAngular(angular, window) {
@@ -225,7 +225,7 @@
225225
'closeButton': false,
226226
'size': '',
227227
'speed': 'steady',
228-
'tooltipTemplateUrlCache': false,
228+
'tooltipTemplateUrlCache': true,
229229
'show': null
230230
};
231231

@@ -571,65 +571,79 @@
571571
registerOnScrollFrom(parentElement);
572572
}
573573
}
574+
, showTemplate = function showTemplate(template) {
575+
576+
tooltipElement.removeClass('_force-hidden'); //see lines below, this forces to hide tooltip when is empty
577+
tipTipElement.empty();
578+
tipTipElement.append(closeButtonElement);
579+
tipTipElement.append(template);
580+
$timeout(function doLater() {
581+
582+
onTooltipShow();
583+
});
584+
}
585+
, hideTemplate = function hideTemplate() {
586+
587+
//hide tooltip because is empty
588+
tipTipElement.empty();
589+
tooltipElement.addClass('_force-hidden'); //force to be hidden if empty
590+
}
591+
, getTemplate = function getTemplate(tooltipTemplateUrl) {
592+
593+
var template = $templateCache.get(tooltipTemplateUrl);
594+
595+
if (typeof template === 'undefined') {
596+
597+
// How should failing to load the template be handled?
598+
template = $http.get(tooltipTemplateUrl).then(function onGetTemplateSuccess(response) {
599+
600+
return response.data;
601+
});
602+
$templateCache.put(tooltipTemplateUrl, template);
603+
}
604+
605+
return template;
606+
}
574607
, onTooltipTemplateChange = function onTooltipTemplateChange(newValue) {
608+
575609
if (newValue) {
576-
tooltipElement.removeClass('_force-hidden'); //see lines below, this forces to hide tooltip when is empty
577-
tipTipElement.empty();
578-
tipTipElement.append(closeButtonElement);
579-
tipTipElement.append(newValue);
580-
$timeout(function doLaterShow() {
581-
582-
onTooltipShow();
583-
});
610+
611+
showTemplate(newValue);
584612
} else {
585-
//hide tooltip because is empty
586-
tipTipElement.empty();
587-
tooltipElement.addClass('_force-hidden'); //force to be hidden if empty
613+
614+
hideTemplate();
588615
}
589616
}
590617
, onTooltipTemplateUrlChange = function onTooltipTemplateUrlChange(newValue) {
618+
591619
if (newValue && !$attrs.tooltipTemplateUrlCache) {
592-
593-
$http.get(newValue).then(function onResponse(response) {
594-
595-
if (response &&
596-
response.data) {
597-
598-
tooltipElement.removeClass('_force-hidden'); //see lines below, this forces to hide tooltip when is empty
599-
tipTipElement.empty();
600-
tipTipElement.append(closeButtonElement);
601-
tipTipElement.append($compile(response.data)(scope));
602-
$timeout(function doLater() {
603-
604-
onTooltipShow();
605-
});
606-
}
620+
621+
getTemplate(newValue).then(function onGetTemplateSuccess(template) {
622+
623+
showTemplate($compile(template)(scope));
624+
}).catch(function onGetTemplateFailure(reason) {
625+
626+
$log.error(reason);
607627
});
608628
} else {
609-
//hide tooltip because is empty
610-
tipTipElement.empty();
611-
tooltipElement.addClass('_force-hidden'); //force to be hidden if empty
629+
630+
hideTemplate();
612631
}
613632
}
614633
, onTooltipTemplateUrlCacheChange = function onTooltipTemplateUrlCacheChange(newValue) {
634+
615635
if (newValue && $attrs.tooltipTemplateUrl) {
636+
637+
getTemplate($attrs.tooltipTemplateUrl).then(function onGetTemplateSuccess(template) {
638+
639+
showTemplate($compile(template)(scope));
640+
}).catch(function onGetTemplateFailure(reason) {
616641

617-
var template = $templateCache.get($attrs.tooltipTemplateUrl);
618-
619-
if (typeof template !== 'undefined') {
620-
621-
tooltipElement.removeClass('_force-hidden'); //see lines below, this forces to hide tooltip when is empty
622-
tipTipElement.empty();
623-
tipTipElement.append(closeButtonElement);
624-
tipTipElement.append($compile(template)(scope));
625-
$timeout(function doLater() {
626-
onTooltipShow();
627-
});
628-
}
642+
$log.error(reason);
643+
});
629644
} else {
630-
//hide tooltip because is empty
631-
tipTipElement.empty();
632-
tooltipElement.addClass('_force-hidden'); //force to be hidden if empty
645+
646+
hideTemplate();
633647
}
634648
}
635649
, onTooltipSideChange = function onTooltipSideChange(newValue) {

0 commit comments

Comments
 (0)