initial data loading in angularjs

All we need is an easy explanation of the problem, so here it is.

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.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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.

Method 2

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?

Method 3

One way to do it is to create a directive that handles the initialization before binding happens.
For example:

app.directive('initdata', function() {
    return {
        restrict: 'A',
        link: function($scope, element, attrs) {
            if ( attrs.ngBind !== undefined)
            {
                $scope[attrs.ngBind] = attrs.initdata ? attrs.initdata : element.text();
            }
        }
    };
});

This directive takes either the attribute value as initial value for the bound $scope property, or the textvalue of the element.

Example usage:

<div initdata="Foo Bar" ng-bind="test1"></div>
<div initdata ng-bind="test2">Lorem Ipsem</div>

Working example on http://jsfiddle.net/6PNG8/

There’s numerous way to elaborate on this; for example parsing the initdata as json and merging it with the scope, and making it work for more complicated binds, like $root.someprop. But the basis is remarkably simple.

Method 4

According to the answers on this question, a JSON object in a script tag on the page seems to be the way to go. If ayone comes up with a better idea, I’ll accept your answer.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply