Skip to content

Commit 937a020

Browse files
committed
updated observer to match modern angular styleguide
1 parent 54b3f5c commit 937a020

File tree

1 file changed

+71
-19
lines changed

1 file changed

+71
-19
lines changed

README.md

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -794,33 +794,85 @@ will produce the same result as the one above. The main difference here is that
794794
795795
![Observer](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/observer.svg "Fig. 7")
796796

797-
There are two basic ways of communication between the scopes in an AngularJS application. The first one is calling methods of parent scope by a child scope. This is possible since the child scope inherits prototypically by its parent, as mentioned above (see [Scope](#scope)). This allows communication in a single direction - child to parent. Some times it is necessary to call method of given child scope or notify it about a triggered event in the context of the parent scope. AngularJS provides built-in observer pattern, which allows this. Another possible use case, of the observer pattern, is when multiple scopes are interested in given event but the scope, in which context the event is triggered, is not aware of them. This allows decoupling between the different scopes, non of the scopes should be aware of the rest of the scopes.
797+
## About
798+
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.
798799

799-
Each AngularJS scope has public methods called `$on`, `$emit` and `$broadcast`. The method `$on` accepts topic as first argument and callback as second. We can think of the callback as an observer - an object, which implements the `Observer` interface (in JavaScript the functions are first-class, so we can provide only implementation of the `notify` method):
800+
**Use Case:** You would use this pattern to communicate between 2 controllers that use the same model but are not connected in anyway
800801

801-
```JavaScript
802-
function ExampleCtrl($scope) {
803-
$scope.$on('event-name', function handler() {
804-
//body
805-
});
806-
}
807-
```
802+
##### How to use
808803

809-
In this way the current scope "subscribes" to events of type `event-name`. When `event-name` is triggered in any parent or child scope of the given one, `handler` would be called.
804+
**Require, angularJS!**
810805

811-
The methods `$emit` and `$broadcast` are used for triggering events respectively upwards and downwards through the scope chain.
812-
For example:
806+
##### Methods
813807

814-
```JavaScript
815-
function ExampleCtrl($scope) {
816-
$scope.$emit('event-name', { foo: 'bar' });
808+
**_observerService.attach = function(callback, event, id)**
809+
810+
function adds a listener to an event with a callback which is stored against the event with it's corresponding id.
811+
812+
**_observerService.detachById = function(id)**
813+
814+
function removes all occurences of one id from all events in the observer object
815+
816+
**_observerService.detachByEvent = function(event)** __most commonly used and least expensive__
817+
818+
function removes all occurences of the event from the observer Object
819+
820+
**_observerService.detachByEventAndId = function(event, id)**
821+
822+
removes all callbacks for an id in a specific event from the observer object
823+
824+
**_observerService.notify = function(event, parameters)**
825+
826+
notifies all observers of a specific event, can pass a params variable of any type
827+
828+
##### Controller Example
829+
Below example shows how to attach, notify and detach an event.
830+
```
831+
angular.module('app.controllers')
832+
.controller('ObserverExample',ObserverExample);
833+
ObserverExample.$inject= ['ObserverService','$timeout'];
834+
function ObserverExample(ObserverService, $timeout) {
835+
var vm = this;
836+
var id = 'vm1';
837+
ObserverService.attach(callbackFunction, 'let_me_know', id)
838+
839+
function callbackFunction(params){
840+
console.log('now i know');
841+
ObserverService.detachByEvent('let_me_know')
842+
}
843+
844+
$timeout(function(){
845+
ObserverService.notify('let_me_know');
846+
},5000);
817847
}
818848
```
849+
Alternative way to remove event
819850

820-
The scope in the example above, triggers the event `event-name` to all scopes upwards. This means that each of the parent scopes of the given one, which are subscribed to the event `event-name`, would be notified and their handler callback will be invoked.
821-
822-
Analogical is the case when the method `$broadcast` is called. The only difference is that the event would be transmitted downwards - to all children scopes.
823-
Each scope can subscribe to any event with multiple callbacks (i.e. it can associate multiple observers to given event).
851+
```
852+
angular.module('app.controllers')
853+
.controller('ObserverExample',ObserverExample);
854+
ObserverExample.$inject= ['ObserverService','$timeout', '$scope'];
855+
function ObserverExample(ObserverService, $timeout, $scope) {
856+
var vm = this;
857+
var id = 'vm1';
858+
ObserverService.attach(callbackFunction, 'let_me_know', id)
859+
860+
function callbackFunction(params){
861+
console.log('now i know');
862+
}
863+
864+
$timeout(function(){
865+
ObserverService.notify('let_me_know');
866+
},5000);
867+
868+
// Cleanup listeners when this controller is destroyed
869+
$scope.$on('$destroy', function handler() {
870+
ObserverService.detachByEvent('let_me_know')
871+
});
872+
}
873+
```
874+
**Public Repository to get you started**
875+
https://github.com/greglbd/angular-observer-pattern
824876

825877
In the JavaScript community this pattern is better known as publish/subscribe.
826878

0 commit comments

Comments
 (0)