4

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?

1
  • 1
    It occurred to me that if I could get my HTML (skeleton) to be static, I could cache the page and the client could avoid the two hits to the server after the first visit. Jan 15, 2014 at 3:10

2 Answers 2

4

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.

1
  • This is the approach I take. The controller which serves the page will get a Json block of data from the same place as the controller which handles the AJAX request; and put it inline in a <script> tag exactly as you have shown here. Jan 14, 2014 at 7:09
-1

The server receives the request and generates the HTML

One-page application should be a skeleton, some views and some models (MV*). I think is incorrect to say that, at the first call, the server generates the HTML. In fact, Angular, or backbone, or any other Javascript frameworks, they want to manipulate the DOM (and they do it very well).

The role of the server, and the first call is to response with a skeleton. From that moment on, the server job is to handle RESTful.

What I'm considering is that building hybrid Web Apps (where an ASP.NET application or PHP meet MV*) can be a little confusing, especially because probably these are generating a lot of HTML in their core.

So the potential of Angular.js (or...) is reduced (mixing both philosophies), just because on the server side, there are still many processes of transformation of the DOM, and the javascript framework is ready to handle this at 100%, but it does so merely in part.

3
  • This doesn't answer my question. It just criticizes my use of the word "generate" and tells me what AngularJS does. The question is: how do I avoid two requests to the server? Jan 15, 2014 at 3:08
  • The criticism comes from, as recommended by Backbone, Angular, Jquery, and many other libraries, the skeleton of your application should be static with libraries, templates, it should be made ​​available in a distributed environment (maybe a CDN). So in my opinion, in a MV* context, the first response from the server 1) does not generate HTML, 2) does not generate load in your application.
    – kedoska
    Jan 15, 2014 at 7:04
  • I'm not disagreeing with what you are saying. But generating a skeleton (which is HTML) does require work on the server unless it is truly static and cache-able. Browsers have limitations on the number of concurrent requests. I was curious whether there was a way to eliminate one of the first hits. Templates have the benefit of being truly static and therefore are cached. Those don't concern me as much as the dynamic content - the skeleton and the API data. @zachary-k answers my question and now I am just hoping for other possible approaches. Jan 15, 2014 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.