Advertisement
Guest User

Untitled

a guest
Aug 8th, 2014
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 6.04 KB | None | 0 0
  1. <html>
  2. <head>
  3.     <style>
  4.         body {
  5.             font-family: 'Arial';
  6.         }
  7.  
  8.         p.bold {
  9.             font-weight: bold;
  10.         }
  11.  
  12.         p.sectionHeader {
  13.             color: #3c5f6c;
  14.             margin-bottom: 5px;
  15.         }
  16.  
  17.         p.marginized {
  18.             margin-bottom: 15px;
  19.         }
  20.  
  21.         .section {
  22.             padding-left: 20px;
  23.             margin-top: 15px;
  24.         }
  25.     </style>
  26. </head>
  27. <body ng-app="myApp">
  28.  
  29.     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.js"></script>
  30.     <script src="https://cdnjs.cloudflare.com/ajax/libs/restangular/1.4.0/restangular.js"></script>
  31.     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
  32.     <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore.js"></script>
  33.     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular-mocks.js"></script>
  34.  
  35.     <!-- companyMain.html -->
  36.     <script type="text/ng-template" id="companyMain.html">
  37.         <p class="bold">Company Main Template</p>
  38.         <div ui-view="summary"></div>
  39.         <div ui-view="locations"></div>
  40.     </script>
  41.     <!-- companyDetails.html -->
  42.     <script type="text/ng-template" id="companyDetails.html">
  43.         <div class="section">
  44.             <p class="bold sectionHeader">Company Details</p>
  45.             <p>{{reportData.name}}</p>
  46.             <p>{{reportData.phone}}</p>
  47.         </div>
  48.     </script>
  49.     <!-- companyLocations.html -->
  50.     <script type="text/ng-template" id="companyLocations.html">
  51.         <div class="section">
  52.             <p class="bold sectionHeader">Company Locations</p>
  53.             <div ng-repeat="location in reportData.locations">
  54.                 <p>Type: {{location.type}}</p>
  55.                 <p class="marginized">Address: {{location.address}}</p>
  56.             </div>
  57.         </div>
  58.     </script>
  59.     <div ng-controller="MyCtrl">
  60.         <p class="bold">Hello, {{name}}!</p>
  61.         <p><a ui-sref="home">Go home</a></p>
  62.         <p>Load the <a ui-sref="company.landing({'id': '12345abcde67890fghij'})">company</a></p><br />
  63.         <div ui-view="main"></div>
  64.     </div>
  65.  
  66.     <script>
  67.         var myApp = angular.module('myApp', ['ngMockE2E', 'ui.router', 'restangular']);
  68.  
  69.         myApp.config(['$urlRouterProvider', '$stateProvider', function config($urlRouterProvider, $stateProvider) {
  70.             $stateProvider
  71.                 .state('company', {
  72.                     abstract: true,
  73.                     url: '/company',
  74.                     data: {
  75.                         pageTitle: 'Company'
  76.                     },
  77.                     views: {
  78.                         'main': {
  79.                             templateUrl: 'companyMain.html'
  80.                         }
  81.                     }
  82.                 })
  83.                 .state('company.landing', {
  84.                     url: '/{id}',
  85.                     views: {
  86.                         'summary': {
  87.                             controller: 'CompanyCtrl',
  88.                             templateUrl: 'companyDetails.html'
  89.                         },
  90.                             'locations': {
  91.                             controller: 'CompanyCtrl',
  92.                             templateUrl: 'companyLocations.html'
  93.                         }
  94.                     },
  95.                     resolve: {
  96.                         // Retrieve a company's report, if the id is present
  97.                         company: ['$log', '$stateParams', 'CompanySvc', function ($log, $stateParams, CompanySvc) {
  98.                             $log.info('In company.landing:resolve and $stateParams is: ', $stateParams);
  99.            
  100.                             var id = $stateParams.id;
  101.            
  102.                             $log.info('In company.landing:resolve and id is: ', id);
  103.                        
  104.                             return CompanySvc.getCompany(id).then(function (response) {
  105.                                 return response;
  106.                             });
  107.                         }]
  108.                     }
  109.                 })
  110.                 .state('home', {
  111.                     url: '/'
  112.                 });
  113.            
  114.             $urlRouterProvider.otherwise('/');
  115.         }]);
  116.  
  117.         myApp.run(['$httpBackend', '$rootScope', function ($httpBackend, $rootScope) {
  118.  
  119.             // handle any route related errors (specifically used to check for hidden resolve errors)
  120.             $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){
  121.                 console.log(error);
  122.             });
  123.  
  124.             var company = {
  125.                 name: 'Big Time Test Co.',
  126.                 phone: '123-456-7890',
  127.                 locations: [
  128.                     {
  129.                         type: 'Primary',
  130.                         address: 'New York, NY'
  131.                     },
  132.                     {
  133.                         type: 'Mailing',
  134.                         address: 'Pueblo, CO'
  135.                     }
  136.                 ]
  137.             };
  138.            
  139.             // Get a mock company report (/int/companies/{companyId}/report)
  140.             $httpBackend.whenGET(/^\/int\/companies\/.+\/report/).respond(company);
  141.         }]);
  142.  
  143.         myApp.factory('CompanySvc', ['$log', 'Restangular', function ($log, Restangular) {
  144.             var restAngular = Restangular.withConfig(function (Configurer) {
  145.                 Configurer.setBaseUrl('/int');
  146.             });
  147.  
  148.             // Object for working with individual Companies
  149.             var _companySvc = restAngular.all('companies');
  150.  
  151.             // Expose our CRUD methods
  152.             return {
  153.                 // GET a single record /int/companies/{id}/report
  154.                 getCompany: function (id) {
  155.                     $log.info('In CompanySvc.getCompany & id is: ', id);
  156.  
  157.                     return restAngular.one('companies', id).customGET('report').then(function success(response) {
  158.                         $log.info('Retrieved company: ', response);
  159.  
  160.                         return response;
  161.                     }, function error(reason) {
  162.                         $log.error('ERROR: retrieving company: ', reason);
  163.  
  164.                         return false;
  165.                     });
  166.                 }
  167.             };
  168.         }]);
  169.  
  170.         myApp.controller('CompanyCtrl', ['$log', '$scope', '$state', 'company', function CompanyCtrl($log, $scope, $state, company) {
  171.             $log.info('In CompanyCtrl & $state is: ', $state);
  172.             $log.info('In CompanyCtrl and company data is: ', company);
  173.             $scope.reportData = company ? company : {};
  174.             $log.info('In CompanyCtrl and $scope.reportData is: ', $scope.reportData);
  175.         }]);
  176.  
  177.         myApp.controller('MyCtrl', ['$scope', function MyCtrl($scope) {
  178.             $scope.name = 'friend';
  179.         }]);
  180.     </script>
  181. </body>
  182. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement