Tutorial 18.1 Service Decorator Example

******************************************INDEX.HTML*****************************************************
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-service-decorator-production</title>
  <link href="style.css" rel="stylesheet" type="text/css">
 

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

 
</head>
<body ng-app="myServiceDecorator">
  <div ng-controller="Ctrl">
  <h1>Logs</h1>
  <my-log></my-log>
</div>
</body>
</html>


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

  controller('Ctrl', [
    '$scope',
    '$log',
    '$timeout',
    function($scope, $log, $timeout) {
      var types = ['error', 'warn', 'log', 'info' ,'debug'], i;

      for (i = 0; i < types.length; i++) {
        $log[types[i]](types[i] + ': message ' + (i + 1));
      }

      $timeout(function() {
        $log.info('info: message logged in timeout');
      });
    }
  ]).

  directive('myLog', [
    '$log',
    function($log) {
      return {
        restrict: 'E',
        template: '<ul id="myLog"><li ng-repeat="l in myLog" class="{{l.type}}">{{l.message}}</li></ul>',
        scope: {},
        compile: function() {
          return function(scope) {
            scope.myLog = $log.stack;
          };
        }
      };
    }
  ]).

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

      $provide.decorator('$log', [
        '$delegate',
        function logDecorator($delegate) {

          var myLog = {
            warn: function(msg) {
              log(msg, 'warn');
            },
            error: function(msg) {
              log(msg, 'error');
            },
            info: function(msg) {
              log(msg, 'info');
            },
            debug: function(msg) {
              log(msg, 'debug');
            },
            log: function(msg) {
              log(msg, 'log');
            },
            stack: []
          };

          function log(msg, type) {
            myLog.stack.push({ type: type, message: msg.toString() });
            if (console && console[type]) console[type](msg);
          }

          return myLog;

        }
      ]);

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

********************************************script.js***************************************************
********************************************style.css***************************************************
li.warn { color: yellow; }
li.error { color: red; }
li.info { color: blue }
li.log { color: black }
li.debug { color: green }
********************************************style.css***************************************************


******************************************Protractor.js*****************************************************
it('should display log messages in dom', function() {
  element.all(by.repeater('l in myLog')).count().then(function(count) {
    expect(count).toEqual(6);
  });
});
******************************************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