template caching bug #455

Closed
tylercollier opened this Issue Feb 9, 2014 · 9 comments

Comments

Projects
None yet
7 participants

I'm not sure if this is a bug with angular-strap. It might be an angularjs bug.

Here's my original question: http://stackoverflow.com/questions/21655633/angular-strap-tooltip-not-working-in-ng-repeat/21656097?noredirect=1#21656097. It mentions this plunker demo: http://plnkr.co/edit/ZRk2q2Xn6hObUFJ5SIkr?p=preview.

As @calebboyd discovered and answered on stackoverflow, the issue is with template caching.

At https://github.com/mgcrea/angular-strap/blob/v2.0.0-rc.2/src/tooltip/tooltip.js#L80-81, the $templateCache is returning with an array of the previous $http.get() call. That's odd; it should be an object with a .data property. Thus the subsequent code fails without a legit template.

I'm using angular 1.2.2 right now, but the problem also exists with 1.2.12 (as shown by my plunker link). I'm guessing this is an angular bug? Can you confirm?

@calebboyd created a quick solution by replacing those two lines with the following code (or see this plunker: http://plnkr.co/edit/C2L0Pyt1eTW7lM2Tt7Yf?p=info)

return $q.when($templateCache.get(options.contentTemplate) && $templateCache.get(options.contentTemplate)[1] || $http.get(options.contentTemplate, { cache: $templateCache })
              .then(function(response){return response.data}))
              .then(function (contentTemplate) {

I fixed it for myself by not using $templateCache but just using

$http.get(options.contentTemplate, { cache: true })

instead of

$http.get(options.contentTemplate, { cache: $templateCache })

I see this has already been changed in master. But It doesn't really make sense to keep the template in $http's $cacheFactory. As the user(dev) might want to use it elsewhere. Why not just use a utility method to do something like:

function getTemplate(template){
    return $http.get(template).then(function(response){
        $templateCache.put(template,response.data);
        return response.data;         
    });
}

It's not fixed in the master dist directory though. See: https://github.com/mgcrea/angular-strap/blob/master/dist/angular-strap.js#L2670.

@mgcrea feel free to close this then but I'm curious as to why you took the caching out altogether with 24545fe. Perhaps you set a default caching option somewhere; I didn't look.

Owner

mgcrea commented Feb 9, 2014

@tylercollier I will issue a new release very soon. Dropped the cache support to follow the global $http configuration defined for any app. Indeed having a getTemplate-like function might be interesting to properly leverage $templateCache.

Awesome. I'll let you close this when you want, or you can leave it open as a reminder. Up to you. Thanks for Angular-Strap and congrats on its success!

mgcrea closed this in aa83097 Feb 9, 2014

morgun commented Sep 12, 2014

Fix is incorrect both 2.0.5 and 2.1.0 are not working with templates cached by ng-include

Correct fix is to simplify fetchTemplate:
function fetchTemplate(template) { return $http.get(template, {cache: $templateCache}); }

Owner

mgcrea commented Sep 12, 2014

@morgun would be great if you could add an unit test for this.

Morriz commented Oct 21, 2014

Can you please fix this asap? It's really creating headaches for me...

Workaround:

Add a random hash version string to your content template url, until this issue is resolved.

contentTemplate: 'myCoolSite/myCoolTemplate.html' + '#' + (new Date).getTime()

Hi, i encountered that bug again. Raised 404 on trying to receive template from cache. Fixed in local library by changing "fetchTemplate" method from early version:

function fetchTemplate(template) { if (fetchPromises[template]) return fetchPromises[template]; return (fetchPromises[template] = $q.when($templateCache.get(template) || $http.get(template)) .then(function (res) { if (angular.isObject(res)) { $templateCache.put(template, res.data); return res.data; } return res; })); }

Could you please help me, whether it may entail some or backfire, or perhaps there is some other way to solve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment