Tutorial 18.3 Filter Decorator Example

******************************************INDEX.HTML*****************************************************
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-filter-decorator-production</title>
 

  <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
  <script src="script.js"></script>
 

 
</head>
<body ng-app="filterDecorator">
  <div ng-controller="Ctrl">
  <div id="genesis">Initial Commit default to short date: {{ genesis | date }}</div>
  <div>ng-conf 2016 default short date: {{ ngConf | date }}</div>
  <div id="ngConf">ng-conf 2016 with full date format: {{ ngConf | date:'fullDate' }}</div>
</div>
</body>
</html>


******************************************INDEX.HTML*****************************************************
********************************************script.js***************************************************
(function(angular) {
  'use strict';
angular.module('filterDecorator', []).

  controller('Ctrl', ['$scope', function($scope) {
    $scope.genesis = new Date(2010, 0, 5);
    $scope.ngConf = new Date(2016, 4, 4);
  }]).

  config(['$provide', function($provide) {

    $provide.decorator('dateFilter', [
      '$delegate',
      function dateDecorator($delegate) {

        // store the original filter
        var originalFilter = $delegate;

        // return our filter
        return shortDateDefault;

        // shortDateDefault sets the format to shortDate if it is falsy
        function shortDateDefault(date, format, timezone) {
          if (!format) format = 'shortDate';

          // return the result of the original filter
          return originalFilter(date, format, timezone);
        }

      }

    ]);

  }]);
})(window.angular);



********************************************script.js***************************************************

******************************************Protractor.js*****************************************************
it('should default date filter to short date format', function() {
  expect(element(by.id('genesis')).getText())
    .toMatch(/Initial Commit default to short date: \d{1,2}\/\d{1,2}\/\d{2}/);
});

it('should still allow dates to be formatted', function() {
  expect(element(by.id('ngConf')).getText())
    .toMatch(/ng-conf 2016 with full date format: [A-Za-z]+, [A-Za-z]+ \d{1,2}, \d{4}/);
});


******************************************Protractor.js*****************************************************

Comments

Popular posts from this blog

Tutorial 12.12 Creating a Directive that Wraps Other Elements 3

Tutorial 12.14 Creating Directives that Communicate