angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Add asynchronous template provider support

Open ovax3 opened this issue 13 years ago • 2 comments

In a current project we load all our partials at once from a using $templateCache. It works fine. This is « asynchronous / sequential » as the Angular app is bootstrapped only after the file is loaded.

We recently needed to load a different set of partials according to the « viewport type » (mobile, tablet, desktop) which is evaluated from the viewport dimensions.

Our Gruntfile is now generating three files : mobile-partial.js, tablet-partial.js and desktop-partials.js. All roughly containing the same set of template URLs.

The bad

At first we thought of using a simple attribute ng-src="{{partialUrl}}", with partialUrl targetting the correct file, but we lost the « asynchronous / sequential » behavior of the browser loading sequentially the different javascript files. This was now « asynchronous / parallel ». As a result ngRoute asked for a template before <script ng-src=... had succefully loaded the partials into $templateCache, and ended in a 404 HTTP error.

The fail is demoed here : http://plnkr.co/edit/qbMgL6Q9A6i1MLkZZG5q

The ugly

Currently a temporary solution is in place which consists of a set of non Angular managed code adding the <script element to the DOM and a tiny piece of code at the end of the ...-partials.js files which manually bootstrap the Angular app when we are sure all the templates have been correctly loaded. I don't like this solution as it is a very constrained and non elegant one.

No demo for this one. We are too ashamed...

The good

In the following demo we have tried a better solution : http://plnkr.co/edit/BWjDJP9Lhq6L7pSKZyac

It consists of decorating the $templateCache in order to delay partials evaluation until all the template HTML code have been loaded from one JSON file.

I like this solution as it allows, to a wider extent, to mix preloading and lazy loading of sets of templates from one or more JSON files (good for performances and user experience). And it allows to « route » partial requests according to the context (viewport size, screen density, language, etc) in a transverse manner without requiring server side operations or specific dynamic partials IDs. This sounds like a great solution for big apps.

The only problem I can see with this solution is that it is an implementation specific and opportunist solution. This is not the kind of things I like to ship to our clients.

It exploits the fact that in most of the Angular code (e.g. ngInclude.js) templates are retrieved this way

http.get('partial.html', { cache: $templateCache }, ....

As a result it is possible to store in/return from $templateCache a promise. But this promise must be a « HTTP result » like promise, i.e. must resolve to this kind of object

{ data: ..., status: 200, headers: {} }

So finally, what would be nice is that Angular publicly allows to return promises from $templateCache and that those promises can simply resolve to a string containing the HTML content of the given template.

If the issue is not rejected too soon I might be able to deliver a PR patch soon.

Thx

ovax3 avatar Sep 12 '13 16:09 ovax3

I know it's old, but +1

pieterbeulque avatar May 06 '16 08:05 pieterbeulque

Just stumbled over this agiain. I think it's a pretty specific requirement, and I think it's reasonable to use a decorator / overwrite if you need this behavior. Specifically, since Angular 1.4, you could implement this in a $templateRequest decorator / overwrite, which would semantically be a better fit, and allow you to ignore the cache logic.

Narretz avatar Nov 03 '16 15:11 Narretz