Skip to content

Commit b2697d6

Browse files
committed
Merge pull request mgechev#15 from greglbd/master
updated observer pattern for modern angular style guide and added examples
2 parents 70ec6b2 + f8cc501 commit b2697d6

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* [Others](#others)
3737
* [Module Pattern](#module-pattern)
3838
* [Data Mapper](#data-mapper)
39+
* [Observer Pattern as an External Service](#observer-pattern-as-an-external-service)
3940
* [References](#references)
4041

4142
<!--endtoc-->
@@ -824,6 +825,8 @@ Each scope can subscribe to any event with multiple callbacks (i.e. it can assoc
824825

825826
In the JavaScript community this pattern is better known as publish/subscribe.
826827

828+
For a best practice example see [Observer Pattern as an External Service](#observer-pattern-as-an-external-service)
829+
827830
#### Chain of Responsibilities
828831

829832
>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:
10881091
</div>
10891092
```
10901093
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+
10911182
## References
10921183
10931184
1. [Wikipedia](https://en.wikipedia.org/wiki). The source of all brief descriptions of the design patterns is wikipedia.

0 commit comments

Comments
 (0)