Johannes Klauß
Johannes Klauß

Reputation: 11020

How to store templates in heavy AJAX based web apps

I'm currently working on a concept for a heavy AJAX based web application.

The communication between client and server should be based on JSON, not plain HTML. So I was wondering how to store the templates for the seperate sections and how to get them by the javascript.

For example I have an multiple image uploading form. The user submits and with JSON response I get back title of the album and the src paths of the images. I want to show the complete photo stream from that JSON.

I never built something like I want know, so I'm trying to be prepared before actual programming comes in.

Are there good JS templating libraries? Can I load the templates after the AJAX call or do I need to load every template on the first page load?

Upvotes: 0

Views: 381

Answers (1)

Joseph
Joseph

Reputation: 119867

  • Load the templates on demand. This saves you bandwidth, memory space as well as HTTP requests.
  • You can load them as plain HTML or as a bunch of templates, wrapped in JSON.
  • Cache them. You should have some sort of caching logic to check first your local cache (localStorage, a JS object on the page etc.) if the template exists. Otherwise, request it from the server.

I suggest using Mustache for templating. As for storage, I suggest using PersistJS. For caching logic, build one yourself.

Upvotes: 1

Related Questions