Avoid Initial AJAX Call for Loading Data

ajaxserver-sidewebsite-performance

Here is the typical sequence of events when someone navigates to a page in my application:

  • The user navigates to my application, including URL parameters and query strings.
  • The server receives the request and generates the HTML
  • On the client, AngularJS grabs any parameters and makes an AJAX call
  • The resulting data is then bound

Personally, I find it a bit wasteful that this requires two hits to the server. It leads to a delay between the HTML being returned and the inputs being populated.

Am I missing something? Is there a way to avoid going back and forth the first time?

Best Answer

I don't know the specifics of angular, but there is no reason to not insert any initial data into the page directly. In Fact backbone suggests doing exactly that. All you would have to do is put in your template something like this

<script>
var initial_data = {json with your data here};
</script>

Where you inject the json from whatever server side framework you are using.

Related Topic