Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initial data loading in angularjs

When building a web app where every page depends on many data sources, what's the best way to fetch the initial bits of data? When I look at twitter, I see the tweets that are visible on page load are in the HTML source, and more tweets are loaded in using AJAX as you scroll down. But there's no convenient way to get data that's already in the DOM to be inserted into the model.

Making a request for the initial data, immediately after page load seams stupid, because you've just made a lot of roundtrips to the server to fetch css, html and javascript. Would it be a bad idea to insert the data into a javascript tag on the page, so a javascript function can add the initial data?

I'm specifically asking for angularjs, but if there's an general technique, please let me know as well.

like image 281
bigblind Avatar asked Aug 06 '13 17:08

bigblind


2 Answers

You'll be referencing your controller anyway on page load, so you won't have to have an inline script tag.

You can either set a default model and use the attribute ng-bind on initial load, or call a function to pass back data.

It's pretty typical to fetch data on load in angularjs.

like image 95
Christopher Marshall Avatar answered Oct 14 '22 19:10

Christopher Marshall


Would it be best to couple Angularjs with an HTTP Client in the backend like Zend_Http_Client or Guzzle to let the server fetch the data. Then, pass the data as json to javascript upon render.

I know Angularjs is designed for Single Page applications. That's why it makes sense that it lazy loads the data.

However, if we're going to move to the approach where we still render the page dynamically and still delegate the task of organizing the content to Angularjs. What framework will be suitable to contain the AngularJS views. Right now, project templates like angular-seed are all static..

That is, the idea is the server serves a page with the embedded json object. Then angular, takes over in the client side, Fetching additional content where needed.

So instead of just a single page of contact (e.g: index.html), we would have several pages like profiles.html, products.html. The help of the backend would be particularly helpful say you have a section which doesn't change often like your username on the top right side of the page. For me, I just think it's better to have these data preloaded in your page and not have to ask the server after the page has been loaded.

As bigblind have noticed, this seems to be the way sites like facebook, gmail, twitter does it. They contain the data embedded on page load. Then, load additional content via services afterwards.

The idea is something like below:

Webservice <---------- Backend------------> Frontend
     <------------------------------------------

Backend delegates the task of querying the webservice to provide initial data in the rendered page to the client. Then client, can directly connect to webservice to fetch additional content.

Using the above setup.. What is the ideal development stack?

like image 2
chrony Avatar answered Oct 14 '22 19:10

chrony