|
36 | 36 | * [Others](#others) |
37 | 37 | * [Module Pattern](#module-pattern) |
38 | 38 | * [Data Mapper](#data-mapper) |
| 39 | + * [Observer Pattern as an External Service](#observer-pattern-as-an-external-service) |
39 | 40 | * [References](#references) |
40 | 41 |
|
41 | 42 | <!--endtoc--> |
@@ -824,6 +825,8 @@ Each scope can subscribe to any event with multiple callbacks (i.e. it can assoc |
824 | 825 |
|
825 | 826 | In the JavaScript community this pattern is better known as publish/subscribe. |
826 | 827 |
|
| 828 | +For a best practice example see [Observer Pattern as an External Service](#observer-pattern-as-an-external-service) |
| 829 | + |
827 | 830 | #### Chain of Responsibilities |
828 | 831 |
|
829 | 832 | >The chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain. |
@@ -1088,6 +1091,94 @@ And the following partial: |
1088 | 1091 | </div> |
1089 | 1092 | ``` |
1090 | 1093 |
|
| 1094 | +### Observer Pattern as an External Service |
| 1095 | +
|
| 1096 | +##### About |
| 1097 | +Below is an example taken from https://github.com/greglbd/angular-observer-pattern. This is an angular factory which reflects the Observer Pattern. It works well with the ControllerAs method of working as it can be much more efficient that $scope.$watch and more specific to a unique scope or object than $emit and $broadcast when used correctly. |
| 1098 | +
|
| 1099 | +**Use Case:** You would use this pattern to communicate between 2 controllers that use the same model but are not connected in anyway |
| 1100 | +
|
| 1101 | +##### Methods |
| 1102 | +
|
| 1103 | +function adds a listener to an event with a callback which is stored against the event with it's corresponding id. |
| 1104 | +``` |
| 1105 | +_observerService.attach = function(callback, event, id) |
| 1106 | +``` |
| 1107 | + |
| 1108 | + |
| 1109 | +function removes all occurences of one id from all events in the observer object |
| 1110 | +``` |
| 1111 | +_observerService.detachById = function(id) |
| 1112 | +``` |
| 1113 | + |
| 1114 | + |
| 1115 | +function removes all occurences of the event from the observer Object |
| 1116 | +``` |
| 1117 | +_observerService.detachByEvent = function(event) |
| 1118 | +``` |
| 1119 | + |
| 1120 | + |
| 1121 | +removes all callbacks for an id in a specific event from the observer object |
| 1122 | +``` |
| 1123 | +_observerService.detachByEventAndId = function(event, id) |
| 1124 | +``` |
| 1125 | + |
| 1126 | + |
| 1127 | +Notifies all observers of a specific event, can pass a params variable of any type |
| 1128 | +``` |
| 1129 | +_observerService.notify = function(event, parameters) |
| 1130 | +``` |
| 1131 | + |
| 1132 | + |
| 1133 | +##### Controller Example |
| 1134 | +Below example shows how to attach, notify and detach an event. |
| 1135 | +```javascript |
| 1136 | +angular.module('app.controllers') |
| 1137 | + .controller('ObserverExample',ObserverExample); |
| 1138 | +ObserverExample.$inject= ['ObserverService','$timeout']; |
| 1139 | +function ObserverExample(ObserverService, $timeout) { |
| 1140 | + var vm = this; |
| 1141 | + var id = 'vm1'; |
| 1142 | + ObserverService.attach(callbackFunction, 'let_me_know', id) |
| 1143 | + |
| 1144 | + function callbackFunction(params){ |
| 1145 | + console.log('now i know'); |
| 1146 | + ObserverService.detachByEvent('let_me_know') |
| 1147 | + } |
| 1148 | + |
| 1149 | + $timeout(function(){ |
| 1150 | + ObserverService.notify('let_me_know'); |
| 1151 | + },5000); |
| 1152 | +} |
| 1153 | +``` |
| 1154 | +Alternative way to remove event |
| 1155 | +
|
| 1156 | +```javascript |
| 1157 | +angular.module('app.controllers') |
| 1158 | + .controller('ObserverExample',ObserverExample); |
| 1159 | +ObserverExample.$inject= ['ObserverService','$timeout', '$scope']; |
| 1160 | +function ObserverExample(ObserverService, $timeout, $scope) { |
| 1161 | + var vm = this; |
| 1162 | + var id = 'vm1'; |
| 1163 | + ObserverService.attach(callbackFunction, 'let_me_know', id) |
| 1164 | + |
| 1165 | + function callbackFunction(params){ |
| 1166 | + console.log('now i know'); |
| 1167 | + } |
| 1168 | + |
| 1169 | + $timeout(function(){ |
| 1170 | + ObserverService.notify('let_me_know'); |
| 1171 | + },5000); |
| 1172 | + |
| 1173 | + // Cleanup listeners when this controller is destroyed |
| 1174 | + $scope.$on('$destroy', function handler() { |
| 1175 | + ObserverService.detachByEvent('let_me_know') |
| 1176 | + }); |
| 1177 | +} |
| 1178 | +``` |
| 1179 | +**Public Repository to get you started** |
| 1180 | +https://github.com/greglbd/angular-observer-pattern |
| 1181 | +
|
1091 | 1182 | ## References |
1092 | 1183 |
|
1093 | 1184 | 1. [Wikipedia](https://en.wikipedia.org/wiki). The source of all brief descriptions of the design patterns is wikipedia. |
|
0 commit comments