Skip to content

Commit eb08af6

Browse files
committed
Update readme
1 parent b2697d6 commit eb08af6

File tree

1 file changed

+38
-66
lines changed

1 file changed

+38
-66
lines changed

README.md

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,90 +1094,62 @@ And the following partial:
10941094
### Observer Pattern as an External Service
10951095
10961096
##### 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.
10981097
1099-
**Use Case:** You would use this pattern to communicate between 2 controllers that use the same model but are not connected in anyway
1098+
Below is an example taken from [here](https://github.com/greglbd/angular-observer-pattern). This is an angular factory which creates a service implementing 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.
11001099
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-
```
1100+
**Use Case:** You would use this pattern to communicate between 2 controllers that use the same model but are not connected in anyway.
11131101
1102+
##### Controller Example
11141103
1115-
function removes all occurences of the event from the observer Object
1116-
```
1117-
_observerService.detachByEvent = function(event)
1118-
```
1104+
Below example shows how to attach, notify and detach an event.
11191105
1106+
```javascript
1107+
angular.module('app.controllers')
1108+
.controller('ObserverExample', ObserverExample);
1109+
ObserverExample.$inject= ['ObserverService', '$timeout'];
11201110

1121-
removes all callbacks for an id in a specific event from the observer object
1122-
```
1123-
_observerService.detachByEventAndId = function(event, id)
1124-
```
1111+
function ObserverExample(ObserverService, $timeout) {
1112+
var vm = this;
1113+
var id = 'vm1';
11251114

1115+
ObserverService.attach(callbackFunction, 'let_me_know', id)
11261116

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-
1117+
function callbackFunction(params){
1118+
console.log('now i know');
1119+
ObserverService.detachByEvent('let_me_know')
1120+
}
11321121

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);
1122+
$timeout(function(){
1123+
ObserverService.notify('let_me_know');
1124+
}, 5000);
11521125
}
11531126
```
11541127
Alternative way to remove event
11551128
11561129
```javascript
11571130
angular.module('app.controllers')
1158-
.controller('ObserverExample',ObserverExample);
1159-
ObserverExample.$inject= ['ObserverService','$timeout', '$scope'];
1131+
.controller('ObserverExample', ObserverExample);
1132+
ObserverExample.$inject= ['ObserverService', '$timeout', '$scope'];
1133+
11601134
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-
});
1135+
var vm = this;
1136+
var id = 'vm1';
1137+
ObserverService.attach(callbackFunction, 'let_me_know', id)
1138+
1139+
function callbackFunction(params){
1140+
console.log('now i know');
1141+
}
1142+
1143+
$timeout(function(){
1144+
ObserverService.notify('let_me_know');
1145+
}, 5000);
1146+
1147+
// Cleanup listeners when this controller is destroyed
1148+
$scope.$on('$destroy', function handler() {
1149+
ObserverService.detachByEvent('let_me_know')
1150+
});
11771151
}
11781152
```
1179-
**Public Repository to get you started**
1180-
https://github.com/greglbd/angular-observer-pattern
11811153
11821154
## References
11831155

0 commit comments

Comments
 (0)