CodeStock: Orchestrating Apps by Composing Angular Directives

CodeStock: Orchestrating Apps by Composing Angular Directives

94bd558238b69c45d3d3e15797ae94f7?s=128

Jeremy Fairbank

July 11, 2015
Tweet

Transcript

  1. 6.

    Who is software for? • Computers • Users • Programmers

    • Me • You http://www.commitstrip.com/en/2015/05/29/always-stuck-somewhere-in-my-head/
  2. 16.

    – Wikipedia “The various visual elements, known as elements of

    design, formal elements, or elements of art, are the vocabulary with which the visual artist composes. These elements in the overall design usually relate to each other and to the whole art work.” What is composition?
  3. 17.

    – Wikipedia “The way in which something is put together

    or arranged. The combination of parts or elements that make up something.” What is composition?
  4. 21.

    Design Principles • Modularity • Separation of concerns (SOC) •

    Loose coupling • High cohesion • Composition over inheritance • Ad nauseam…
  5. 23.

    Directives Review • Attach behavior to the DOM via attributes,

    elements, comments, or CSS classes • Componentize functionality with HTML • Create custom HTML elements
  6. 24.

    Directives Review • Attach behavior to the DOM via attributes,

    elements, comments, or CSS classes • Componentize functionality with HTML • Create custom HTML elements <my-hello-world en="es"></my-hello-world> <name-tag name="Jeremy"></name-tag>
  7. 25.

    Directives Review angular.module('myApp', []) .directive('nameTag', function() { return { restrict:

    'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; });
  8. 26.

    Directives Review angular.module('myApp', []) .directive('nameTag', function() { return { restrict:

    'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; });
  9. 27.

    Directives Review angular.module('myApp', []) .directive('nameTag', function() { return { restrict:

    'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; });
  10. 28.

    Directives Review angular.module('myApp', []) .directive('nameTag', function() { return { restrict:

    'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; });
  11. 29.

    Directives Review <p> <name-tag name="Jeremy Fairbank"></name-tag> <!-- Hello, my name

    is Jeremy Fairbank --> </p> <p> <name-tag></name-tag> <!-- Hello, my name is Joe Schmoe --> </p>
  12. 30.

    Directives Review <p> <name-tag name="Jeremy Fairbank"></name-tag> <!-- Hello, my name

    is Jeremy Fairbank --> </p> <p> <name-tag></name-tag> <!-- Hello, my name is Joe Schmoe --> </p>
  13. 31.

    Directives Review <p> <name-tag name="Jeremy Fairbank"></name-tag> <!-- Hello, my name

    is Jeremy Fairbank --> </p> <p> <name-tag></name-tag> <!-- Hello, my name is Joe Schmoe --> </p>
  14. 32.

    Directives Review return { restrict: 'E', scope: { name: '@'

    }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } };
  15. 33.

    Directives Review return { restrict: 'E', scope: { name: '@'

    }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; Restrict to element declaration (i.e. `<name-tag></name-tag>`) E - element A - attribute C - class name
  16. 34.

    Directives Review return { restrict: 'E', scope: { name: '@'

    }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; Isolate scope @ - attributes = - bindings & - behavior/callbacks
  17. 35.

    Directives Review return { restrict: 'E', scope: { name: '@'

    }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; Templates template - literal template templateUrl - url or script id for template
  18. 36.

    Directives Review return { restrict: 'E', scope: { name: '@'

    }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; link manipulate DOM or set some defaults on scope controller add to scope too, preferably for exposing an API Set up
  19. 37.

    Directives Review return { restrict: 'E', scope: { name: '@'

    }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; <p> <name-tag name="Jeremy Fairbank"></name-tag> <!-- Hello, my name is Jeremy Fairbank --> </p> <p> <name-tag></name-tag> <!-- Hello, my name is Joe Schmoe --> </p>
  20. 38.

    Directives Review return { restrict: 'E', scope: { name: '@'

    }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; <p> <name-tag name="Jeremy Fairbank"></name-tag> <!-- Hello, my name is Jeremy Fairbank --> </p> <p> <name-tag></name-tag> <!-- Hello, my name is Joe Schmoe --> </p> Supplying the attribute value
  21. 39.

    return { restrict: 'E', scope: { name: '@' }, template:

    'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; <p> <name-tag name="Jeremy Fairbank"></name-tag> <!-- Hello, my name is Jeremy Fairbank --> </p> <p> <name-tag></name-tag> <!-- Hello, my name is Joe Schmoe --> </p> Directives Review Using the default value
  22. 41.

    Composition and Angular • Focus on smaller problems to solve

    a large problem • Apply design principles to directives • Build app in terms of directives • Decompose large directives into small reusable directives • Refactoring and adding new features becomes more manageable
  23. 43.
  24. 45.

    Data Flow • Connecting directives to build an “app” through

    the flow of data • Handling model data among directives • Many approaches with pros and cons App Directive Directive Directive Directive Directive Directive Directive Directive Directive
  25. 46.

    Data Flow 1. Shared/global state with prototypal scoping 2. Exporting

    API’s with directive controllers 3. Isolate scope bindings and callbacks 4. Event broadcasting and emitting App Directive Directive Directive Directive Directive Directive Directive Directive Directive
  26. 49.

    Prototypal Scoping app.directive('imageGallery', function() { return { scope: true, templateUrl:

    'image-gallery.html' }; }); Create a child scope that “inherits” from parent scope
  27. 50.

    Prototypal Scoping app.controller('ImageCtrl', function($scope) { function createImage(src, text) { return

    { src: src, text: text, favorited: false }; } $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; });
  28. 51.

    Prototypal Scoping app.controller('ImageCtrl', function($scope) { function createImage(src, text) { return

    { src: src, text: text, favorited: false }; } $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; });
  29. 52.

    Prototypal Scoping app.controller('ImageCtrl', function($scope) { function createImage(src, text) { return

    { src: src, text: text, favorited: false }; } $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; });
  30. 57.

    Prototypal Scoping <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image in

    images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div> </script>
  31. 58.

    Prototypal Scoping <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image in

    images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div> </script>
  32. 59.

    Prototypal Scoping <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image in

    images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div> </script>
  33. 60.

    Prototypal Scoping <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image in

    images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div> </script>
  34. 61.

    Prototypal Scoping <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li

    ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul> </script>
  35. 62.

    Prototypal Scoping <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li

    ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul> </script>
  36. 63.

    Prototypal Scoping <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li

    ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul> </script>
  37. 65.

    Prototypal Scoping app.directive('imageFavorites', function() { return { scope: true, templateUrl:

    'image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });
  38. 66.

    Prototypal Scoping app.directive('imageFavorites', function() { return { scope: true, templateUrl:

    'image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });
  39. 68.

    Prototypal Scoping What about more than one gallery? <body ng-controller="ImageCtrl">

    <div ng-controller="CatImageCtrl"> <image-gallery></image-gallery> </div> <div ng-controller="NatureImageCtrl"> <image-gallery></image-gallery> </div> </body>
  40. 69.

    Prototypal Scoping What about more than one gallery? <body ng-controller="ImageCtrl">

    <div ng-controller="CatImageCtrl"> <image-gallery></image-gallery> </div> <div ng-controller="NatureImageCtrl"> <image-gallery></image-gallery> </div> </body>
  41. 70.

    Prototypal Scoping app.controller('CatImageCtrl', function($scope) { $scope.images = [ createImage('/images/cat-1.jpg', 'Cat

    1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; }).controller('NatureImageCtrl', function($scope) { $scope.images = [ createImage('/images/nature-1.jpg', 'Nature 1'), createImage('/images/nature-2.jpg', 'Nature 2'), createImage('/images/nature-3.jpg', 'Nature 3'), createImage('/images/nature-4.jpg', 'Nature 4') ]; });
  42. 74.

    app.controller('ImageCtrl', function($scope) { // ... $scope.mainImage = {}; $scope.imageFavorites =

    []; }); app.directive('imageList', function() { return { // ... link: function(scope) { scope.setMainImage = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.imageFavorites.push(image); } }; } }; });
  43. 75.

    app.controller('ImageCtrl', function($scope) { // ... $scope.mainImage = {}; $scope.imageFavorites =

    []; }); app.directive('imageList', function() { return { // ... link: function(scope) { scope.setMainImage = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.imageFavorites.push(image); } }; } }; });
  44. 76.

    app.controller('ImageCtrl', function($scope) { // ... $scope.mainImage = {}; $scope.imageFavorites =

    []; }); app.directive('imageList', function() { return { // ... link: function(scope) { scope.setMainImage = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.imageFavorites.push(image); } }; } }; });
  45. 77.

    app.controller('CatImageCtrl', function($scope) { $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images

    = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; }).controller('NatureImageCtrl', function($scope) { $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/nature-1.jpg', 'Nature 1'), createImage('/images/nature-2.jpg', 'Nature 2'), createImage('/images/nature-3.jpg', 'Nature 3'), createImage('/images/nature-4.jpg', 'Nature 4') ]; });
  46. 78.

    app.controller('CatImageCtrl', function($scope) { $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images

    = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; }).controller('NatureImageCtrl', function($scope) { $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/nature-1.jpg', 'Nature 1'), createImage('/images/nature-2.jpg', 'Nature 2'), createImage('/images/nature-3.jpg', 'Nature 3'), createImage('/images/nature-4.jpg', 'Nature 4') ]; });
  47. 80.

    Prototypal Scoping • Reusable functions • Perfect when sharing data

    is desired • Shared data is problematic when reusing directive • Fixing shared data issues duplicates code • Assumptions about parent scopes
  48. 83.

    Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery',

    scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; });
  49. 84.

    Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery',

    scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; });
  50. 85.

    Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery',

    scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; }); Directive name
  51. 86.

    Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery',

    scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; }); Parent
  52. 87.

    Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery',

    scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; });
  53. 88.

    Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery',

    scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; });
  54. 89.

    Requiring Directive Controllers app.directive('imageGallery', function() { return { scope: true,

    templateUrl: 'templates/image-gallery.html', controller: function() { // ... } }; });
  55. 90.

    Requiring Directive Controllers app.directive('imageGallery', function() { return { scope: true,

    templateUrl: 'templates/image-gallery.html', controller: function() { // ... } }; });
  56. 91.

    controller: function() { this.mainImage = {}; this.imageFavorites = []; this.setMainImage

    = function(image) { this.mainImage.src = image.src; }; this.favorite = function(image) { if (!image.favorited) { image.favorited = true; this.imageFavorites.push(image); } }; this.unfavorite = function(image) { var i = this.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; this.imageFavorites.splice(i, 1); } }; }
  57. 92.

    controller: function() { this.mainImage = {}; this.imageFavorites = []; this.setMainImage

    = function(image) { this.mainImage.src = image.src; }; this.favorite = function(image) { if (!image.favorited) { image.favorited = true; this.imageFavorites.push(image); } }; this.unfavorite = function(image) { var i = this.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; this.imageFavorites.splice(i, 1); } }; }
  58. 93.

    controller: function() { this.mainImage = {}; this.imageFavorites = []; this.setMainImage

    = function(image) { this.mainImage.src = image.src; }; this.favorite = function(image) { if (!image.favorited) { image.favorited = true; this.imageFavorites.push(image); } }; this.unfavorite = function(image) { var i = this.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; this.imageFavorites.splice(i, 1); } }; }
  59. 94.

    Requiring Directive Controllers app.directive('mainImage', function() { return { require: '^imageGallery',

    // ... link: function(scope, el, attrs, ctrl) { scope.ctrl = ctrl; } } }).directive('imageFavorites', function() { return { require: '^imageGallery', // ... link: function(scope, el, attrs, ctrl) { scope.ctrl = ctrl; } }; });
  60. 97.

    Requiring Directive Controllers <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image

    in images"> <div ng-click="ctrl.favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="ctrl.setMainImage(image)"> Set Main Image </button> </div> </script>
  61. 98.

    Requiring Directive Controllers <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image

    in images"> <div ng-click="ctrl.favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="ctrl.setMainImage(image)"> Set Main Image </button> </div> </script>
  62. 99.

    Requiring Directive Controllers <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <h4>Favorited: {{ctrl.imageFavorites.length}}</h4> <ul>

    <li ng-repeat="image in ctrl.imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="ctrl.unfavorite(image)"> Unfavorite </button> </p> </li> </ul> </script>
  63. 100.

    Requiring Directive Controllers <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <h4>Favorited: {{ctrl.imageFavorites.length}}</h4> <ul>

    <li ng-repeat="image in ctrl.imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="ctrl.unfavorite(image)"> Unfavorite </button> </p> </li> </ul> </script>
  64. 102.

    Requiring Directive Controllers • Consolidate behavior into a controller API

    • Keep own copy of data instead of sharing • Higher coupling — child directives depend upon parent directive • Still need a parent controller to scope own images (cat versus nature)
  65. 104.

    Isolate Scopes • Directive scope is isolated from other scopes

    – No parent scope to inherit from – Can’t access other scopes “easily” • Use object literal for scope definition scope: { age: '@age', name: '=', selectHobby: '&onSelectHobby' }
  66. 106.

    scope: { name: '=', age: '@age', selectHobby: '&onSelectHobby' } <name-tag

    name="scopeName" age="28" on-select-hobby="printHobby(hobby)"></name-tag> Isolate Scopes Explicitly specify HTML attribute
  67. 107.

    scope: { name: '=', age: '@age', selectHobby: '&onSelectHobby' } <name-tag

    name="scopeName" age="28" on-select-hobby="printHobby(hobby)"></name-tag> Isolate Scopes Implicit — use the scope key for HTML attribute
  68. 108.

    Isolate Scopes <name-tag name="scopeName" age="28" on-select-hobby="printHobby(hobby)"></name-tag> scope: { name: '=',

    age: '@age', selectHobby: '&onSelectHobby' } Bidirectional data binding Use property scopeName from current scope
  69. 109.

    Isolate Scopes <name-tag name="scopeName" age="28" on-select-hobby="printHobby(hobby)"></name-tag> scope: { name: '=',

    age: '@age', selectHobby: '&onSelectHobby' } Attribute/string binding; static. Use literal string "28"
  70. 110.

    Isolate Scopes <name-tag name="scopeName" age="28" on-select-hobby="printHobby(hobby)"></name-tag> scope: { name: '=',

    age: '@age', selectHobby: '&onSelectHobby' } Execute code in a “parent” scope (i.e. a callback) Function call with any parameter names. Uses function printHobby on parent scope (not the directive scope).
  71. 111.

    Isolate Scopes app.directive('nameTag', function() { return { scope: { age:

    '@age', name: '=', selectHobby: '&onSelectHobby' }, templateUrl: 'name-tag.html', link: function(scope, el, attrs) { scope.hobbies = ['program', 'read', 'play guitar']; scope.age = attrs.age; } }; });
  72. 112.

    Isolate Scopes <script id="name-tag.html" type="text/ng-template"> <p>Hi, my name is {{name}}.

    I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul> </script>
  73. 113.

    Isolate Scopes <script id="name-tag.html" type="text/ng-template"> <p>Hi, my name is {{name}}.

    I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul> </script>
  74. 114.

    Isolate Scopes <script id="name-tag.html" type="text/ng-template"> <p>Hi, my name is {{name}}.

    I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul> </script>
  75. 115.

    Isolate Scopes <script id="name-tag.html" type="text/ng-template"> <p>Hi, my name is {{name}}.

    I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul> </script>
  76. 116.

    Isolate Scopes <name-tag name="scopeName" age="28" on-select-hobby="printHobby(hobby)"></name-tag> <script id="name-tag.html" type="text/ng-template"> <p>Hi,

    my name is {{name}}. I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul> </script>
  77. 117.

    <script id="name-tag.html" type="text/ng-template"> <p>Hi, my name is {{name}}. I am

    {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul> </script> Isolate Scopes <name-tag name="scopeName" age="28" on-select-hobby="printHobby(hobby)"></name-tag> Invocations require “named arguments” via object literal.
  78. 121.

    Isolate Scopes app.controller('ImageCtrl', function($scope) { $scope.catImages = [ createImage('/images/cat-1.jpg', 'Cat

    1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; $scope.natureImages = [ createImage('/images/nature-1.jpg', 'Nature 1'), createImage('/images/nature-2.jpg', 'Nature 2'), createImage('/images/nature-3.jpg', 'Nature 3'), createImage('/images/nature-4.jpg', 'Nature 4') ]; });
  79. 122.

    Isolate Scopes app.directive('imageGallery', function() { return { scope: { images:

    '=' }, templateUrl: 'templates/image-gallery.html', link: function(scope) { // ... } }; });
  80. 123.

    Isolate Scopes app.directive('imageGallery', function() { return { scope: { images:

    '=' }, templateUrl: 'templates/image-gallery.html', link: function(scope) { // ... } }; });
  81. 124.

    Isolate Scopes link: function(scope) { scope.mainImage = {}; scope.imageFavorites =

    []; scope.setMain = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { scope.imageFavorites.push(image); }; scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { scope.imageFavorites.splice(i, 1); } }; }
  82. 125.

    Isolate Scopes link: function(scope) { scope.mainImage = {}; scope.imageFavorites =

    []; scope.setMain = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { scope.imageFavorites.push(image); }; scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { scope.imageFavorites.splice(i, 1); } }; }
  83. 126.

    Isolate Scopes link: function(scope) { scope.mainImage = {}; scope.imageFavorites =

    []; scope.setMain = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { scope.imageFavorites.push(image); }; scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { scope.imageFavorites.splice(i, 1); } }; }
  84. 127.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  85. 128.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  86. 129.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  87. 130.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  88. 131.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  89. 132.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  90. 133.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  91. 134.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  92. 135.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  93. 136.

    Isolate Scopes <script id="image-gallery.html" type="text/ng-template"> <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)"

    on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites> </script>
  94. 137.

    Isolate Scopes app.directive('mainImage', function() { return { scope: { mainImage:

    '=image' }, templateUrl: 'templates/main-image.html' } });
  95. 138.

    Isolate Scopes app.directive('mainImage', function() { return { scope: { mainImage:

    '=image' }, templateUrl: 'templates/main-image.html' } });
  96. 141.

    Isolate Scopes app.directive('imageFavorites', function() { return { scope: { imageFavorites:

    '=images', triggerUnfavorite: '&onUnfavorite' }, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { image.favorited = false; scope.triggerUnfavorite({ image: image }); }; } }; });
  97. 142.

    Isolate Scopes app.directive('imageFavorites', function() { return { scope: { imageFavorites:

    '=images', triggerUnfavorite: '&onUnfavorite' }, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { image.favorited = false; scope.triggerUnfavorite({ image: image }); }; } }; });
  98. 143.

    Isolate Scopes app.directive('imageFavorites', function() { return { scope: { imageFavorites:

    '=images', triggerUnfavorite: '&onUnfavorite' }, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { image.favorited = false; scope.triggerUnfavorite({ image: image }); }; } }; });
  99. 144.

    Isolate Scopes <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li

    ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul> </script>
  100. 145.

    Isolate Scopes <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li

    ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul> </script>
  101. 146.

    Isolate Scopes <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li

    ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul> </script>
  102. 147.

    app.directive('imageList', function() { return { scope: { images: '=', setMainImage:

    '&onSetMain', triggerFavorite: '&onFavorite' }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage({ image: scope.images[0] }); scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.triggerFavorite({ image: image }); } }; } }; });
  103. 148.

    app.directive('imageList', function() { return { scope: { images: '=', setMainImage:

    '&onSetMain', triggerFavorite: '&onFavorite' }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage({ image: scope.images[0] }); scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.triggerFavorite({ image: image }); } }; } }; });
  104. 149.

    app.directive('imageList', function() { return { scope: { images: '=', setMainImage:

    '&onSetMain', triggerFavorite: '&onFavorite' }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage({ image: scope.images[0] }); scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.triggerFavorite({ image: image }); } }; } }; });
  105. 150.

    app.directive('imageList', function() { return { scope: { images: '=', setMainImage:

    '&onSetMain', triggerFavorite: '&onFavorite' }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage({ image: scope.images[0] }); scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.triggerFavorite({ image: image }); } }; } }; });
  106. 151.

    Isolate Scopes <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image in

    images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage({ image: image })"> Set Main Image </button> </div> </script>
  107. 152.

    Isolate Scopes <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image in

    images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage({ image: image })"> Set Main Image </button> </div> </script>
  108. 153.

    Isolate Scopes <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image in

    images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage({ image: image })"> Set Main Image </button> </div> </script>
  109. 155.

    Isolate Scopes • Black box a directive to be self-contained

    and reusable • Think in terms of small pieces • Establish well-defined interface/API through HTML attributes • Juggle multiple points of state • More markup
  110. 156.
  111. 157.

    Events • Built in eventing with $emit, $broadcast, and $on

    methods of $scope. • Use events like a message bus to transfer data • Events travel vertically through scopes – Use parent scope event delegation for sibling directive communication
  112. 158.

    Events $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); // In a child

    directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); });
  113. 159.

    $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); // In a child directive

    var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Events $emit sends events up to parent scopes.
  114. 160.

    $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); // In a child directive

    var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Events $broadcast sends events down to child scopes.
  115. 161.

    $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); // In a child directive

    var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Events $on listens for $emit and $broadcast events and triggers the provided callbacks.
  116. 162.

    // In a child directive var items = []; $scope.$on('add:item',

    function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events The first parameter is an event object like DOM events.
  117. 163.

    // In a child directive var items = []; $scope.$on('add:item',

    function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events Remaining parameters are the remaining arguments supplied to $emit and $broadcast.
  118. 164.

    $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events // In a child

    directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Stick to a naming convention like verb:noun.
  119. 165.

    $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events // In a child

    directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Verb
  120. 166.

    $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events // In a child

    directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Noun
  121. 169.

    Events <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image in images">

    <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div> </script>
  122. 170.

    Events <script id="image-list.html" type="text/ng-template"> <h3>Images</h3> <div class="image-info" ng-repeat="image in images">

    <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div> </script>
  123. 171.

    app.directive('imageList', function() { return { scope: { images: '=', },

    templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage = function(image) { scope.$emit('delegate:set:mainImage', image); }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.$emit('delegate:favorite:image', image); } }; scope.setMainImage(scope.images[0]); } }; });
  124. 172.

    app.directive('imageList', function() { return { scope: { images: '=', },

    templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage = function(image) { scope.$emit('delegate:set:mainImage', image); }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.$emit('delegate:favorite:image', image); } }; scope.setMainImage(scope.images[0]); } }; });
  125. 173.

    app.directive('imageList', function() { return { scope: { images: '=', },

    templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage = function(image) { scope.$emit('delegate:set:mainImage', image); }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.$emit('delegate:favorite:image', image); } }; scope.setMainImage(scope.images[0]); } }; });
  126. 174.

    app.directive('imageList', function() { return { scope: { images: '=', },

    templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage = function(image) { scope.$emit('delegate:set:mainImage', image); }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.$emit('delegate:favorite:image', image); } }; scope.setMainImage(scope.images[0]); } }; });
  127. 175.

    Events app.directive('imageGallery', function() { return { scope: { images: '='

    }, templateUrl: 'templates/image-gallery.html', link: function(scope) { scope.$on('delegate:set:mainImage', function(e, image) { scope.$broadcast('set:mainImage', image); }); scope.$on('delegate:favorite:image', function(e, image) { scope.$broadcast('favorite:image', image); }); } }; });
  128. 176.

    Events app.directive('imageGallery', function() { return { scope: { images: '='

    }, templateUrl: 'templates/image-gallery.html', link: function(scope) { scope.$on('delegate:set:mainImage', function(e, image) { scope.$broadcast('set:mainImage', image); }); scope.$on('delegate:favorite:image', function(e, image) { scope.$broadcast('favorite:image', image); }); } }; });
  129. 177.

    Events app.directive('mainImage', function() { return { scope: {}, templateUrl: 'templates/main-image.html',

    link: function(scope) { scope.mainImage = {}; scope.$on('set:mainImage', function(e, image) { scope.mainImage.src = image.src; }); } } });
  130. 178.

    Events app.directive('mainImage', function() { return { scope: {}, templateUrl: 'templates/main-image.html',

    link: function(scope) { scope.mainImage = {}; scope.$on('set:mainImage', function(e, image) { scope.mainImage.src = image.src; }); } } });
  131. 179.

    Events app.directive('mainImage', function() { return { scope: {}, templateUrl: 'templates/main-image.html',

    link: function(scope) { scope.mainImage = {}; scope.$on('set:mainImage', function(e, image) { scope.mainImage.src = image.src; }); } } });
  132. 180.

    Events app.directive('mainImage', function() { return { scope: {}, templateUrl: 'templates/main-image.html',

    link: function(scope) { scope.mainImage = {}; scope.$on('set:mainImage', function(e, image) { scope.mainImage.src = image.src; }); } } });
  133. 181.

    app.directive('imageFavorites', function() { return { scope: {}, templateUrl: 'templates/image-favorites.html', link:

    function(scope) { scope.imageFavorites = []; scope.$on('favorite:image', function(e, image) { scope.imageFavorites.push(image); }); scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });
  134. 182.

    app.directive('imageFavorites', function() { return { scope: {}, templateUrl: 'templates/image-favorites.html', link:

    function(scope) { scope.imageFavorites = []; scope.$on('favorite:image', function(e, image) { scope.imageFavorites.push(image); }); scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });
  135. 183.

    app.directive('imageFavorites', function() { return { scope: {}, templateUrl: 'templates/image-favorites.html', link:

    function(scope) { scope.imageFavorites = []; scope.$on('favorite:image', function(e, image) { scope.imageFavorites.push(image); }); scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });
  136. 184.

    app.directive('imageFavorites', function() { return { scope: {}, templateUrl: 'templates/image-favorites.html', link:

    function(scope) { scope.imageFavorites = []; scope.$on('favorite:image', function(e, image) { scope.imageFavorites.push(image); }); scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });
  137. 186.

    Events • Decouple directives • Isolate state more easily •

    Cleaner markup • Indirection of data flow (who responds to an event?) • Have to remember and duplicate event names (may hurt directive reuse)
  138. 187.
  139. 189.

    Refactoring • Easier to change or update app • Don’t

    have to touch entire app, just an individual directive – Touching multiple pieces may mean better SOC needed – SOC is evolutionary for the lifetime of the app
  140. 190.

    • Remove count • Remove image text • Sort images

    by text Alter image gallery favorites Refactoring Demo
  141. 191.

    Refactoring Alter image gallery favorites <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <ul>

    <li ng-repeat="image in imageFavorites | orderBy: 'text'"> <p> <img ng-src="{{image.src}}" width="40"> </p> <button ng-clik="unfavorite(image)"> Unfavorite </button> </li> </ul> </script>
  142. 192.

    <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <ul> <li ng-repeat="image in imageFavorites |

    orderBy: 'text'"> <p> <img ng-src="{{image.src}}" width="40"> </p> <button ng-clik="unfavorite(image)"> Unfavorite </button> </li> </ul> </script> Refactoring Alter image gallery favorites Favorites count gone
  143. 193.

    Refactoring Alter image gallery favorites <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <ul>

    <li ng-repeat="image in imageFavorites | orderBy: 'text'"> <p> <img ng-src="{{image.src}}" width="40"> </p> <button ng-clik="unfavorite(image)"> Unfavorite </button> </li> </ul> </script> Image text gone
  144. 194.

    Refactoring Alter image gallery favorites <script id="image-favorites.html" type="text/ng-template"> <h3>Favorites</h3> <ul>

    <li ng-repeat="image in imageFavorites | orderBy: 'text'"> <p> <img ng-src="{{image.src}}" width="40"> </p> <button ng-clik="unfavorite(image)"> Unfavorite </button> </li> </ul> </script>