Monday, September 8, 2014

Extracting the country details query into a service factory callback method with cacheData in angular js

<html ng-app="countryApp">
  <head>
    <meta charset="utf-8">
    <title>Angular.js Example</title>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.10/angular.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.10/angular-route.min.js"></script>
    <script>
      var countryApp = angular.module('countryApp', ['ngRoute']);

      countryApp.config(function($routeProvider) {
        $routeProvider.
          when('/', {
            templateUrl: 'country-list.html',
            controller: 'CountryListCtrl'
          }).
          when('/:countryName', {
            templateUrl: 'country-detail.html',
            controller: 'CountryDetailCtrl'
          }).
          otherwise({
            redirectTo: '/'
          });
      });

      countryApp.factory('countries', function($http){

        var cachedData;

        function getData(callback){
          if(cachedData) {
            callback(cachedData);
          } else {
            $http.get('countries.json').success(function(data){
              cachedData = data;
              callback(data);
            });
          }
        }

        return {
          list: getData,
          find: function(name, callback){
            getData(function(data) {
              var country = data.filter(function(entry){
                return entry.name === name;
              })[0];
              callback(country);
            });
          }
        };
      });

      countryApp.controller('CountryListCtrl', function ($scope, countries){
        countries.list(function(countries) {
          $scope.countries = countries;
        });
      });

      countryApp.controller('CountryDetailCtrl', function ($scope, $routeParams, countries){
        countries.find($routeParams.countryName, function(country) {
          $scope.country = country;
        });
      });
    </script>
  </head>
  <body>
    <div ng-view></div>
  </body>
</html>

or use cache option

function getData(callback){
          $http({
            method: 'GET',
            url: 'countries.json',
            cache: true
          }).success(callback);
        }


country-detail.html

<h1>{{country.name}}</h1>
<ul>
  <li>Flag: <img ng-src="{{country.flagURL}}" width="100"></li>
  <li>Population: {{country.population | number }}</li>
  <li>Capital: {{country.capital}}</li>
  <li>GDP: {{country.gdp | currency }}</li>
</ul>


country-list.html

<ul>
  <li ng-repeat="country in countries">
    <a href="#/{{country.name}}">{{country.name}}</a>
  </li>
</ul>


countries.json

[
  {
    "name": "China",
    "population": 1359821000,
    "flagURL": "//upload.wikimedia.org/wikipedia/commons/f/fa/Flag_of_the_People%27s_Republic_of_China.svg",
    "capital": "Beijing",
    "gdp": 12261
  },
  {
    "name": "India",
    "population": 1205625000,
    "flagURL": "//upload.wikimedia.org/wikipedia/en/4/41/Flag_of_India.svg",
    "capital": "New Delhi",
    "gdp": 4716
  },
  {
    "name": "United States of America",
    "population": 312247000,
    "flagURL": "//upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg",
    "capital": "Washington, D.C.",
    "gdp": 16244
  }
]

No comments: