Created
August 25, 2016 22:16
-
-
Save diestrin/600a1d67b06ec0af9cc68c2f2ab2c079 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function () { | |
| 'use strict'; | |
| function ConsumerController ($scope, Service) { | |
| this.init = function () { | |
| Service.subscribeToEventName($scope, function (e, data) { | |
| // do something | |
| }, this); | |
| }; | |
| this.init(); | |
| } | |
| ConsumerController.$inject = [ | |
| '$scope', | |
| 'Service' | |
| ]; | |
| angular.module('Consumer') | |
| .controller('consumer', ConsumerController); | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function () { | |
| 'use strict'; | |
| /** | |
| * This service is used to handle global events inside services, without the | |
| * need to subscribe or trigger events in different parts of code | |
| */ | |
| function CustomEventFactory ($window, $rootScope) { | |
| var CustomEvent = { | |
| /** | |
| * This method attach the vent names subscribeTo* and notify* to the | |
| * context object where it's called | |
| * | |
| * @param {string} eventName The event name identifier | |
| * @param {object} context The object on where the events are going | |
| * to be attached | |
| */ | |
| attach: function (eventName, context) { | |
| /** | |
| * subscribeTo{event} subscribe to an specific event of the object | |
| * | |
| * @param {$scope} $scope The subscriber' scope, this is to allow | |
| * de-subscription on scope $destroy event | |
| * @param {Function} fn Callback function to call | |
| * @param {Any} fnCtx Context for the callback function | |
| * | |
| * @return {Function} Handler object for manual de-subscription | |
| */ | |
| context['subscribeTo' + eventName] = function($scope, fn, fnCtx) { | |
| var handler = $rootScope.$on(eventName, fn.bind(fnCtx)); | |
| $scope.$on('$destroy', handler); | |
| return handler; | |
| }; | |
| /** | |
| * notify{event} sends a notification to every subscriber on this event | |
| * | |
| * @param {Any} data The information to send, if any | |
| */ | |
| context['notify' + eventName] = function (data) { | |
| $rootScope.$emit(eventName, data); | |
| }; | |
| } | |
| }; | |
| return CustomEvent; | |
| } | |
| CustomEventFactory.$inject = ['$window', '$rootScope']; | |
| angular.module('CustomEvent', []) | |
| .factory('CustomEvent', CustomEventFactory); | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function () { | |
| 'use strict'; | |
| /** | |
| * This service centralize the operations about the current station | |
| */ | |
| function ServiceFactory (WUCustomEvent) { | |
| var Service = { | |
| doSomething: function (param) { | |
| return returnPromise() | |
| .then(function (result) { | |
| Service.notifyStationChanged(result); | |
| }); | |
| } | |
| }; | |
| CustomEvent.attach('EventName', Service); | |
| return wuStation; | |
| } | |
| ServiceFactory.$inject = [ | |
| 'CustomEvent' | |
| ]; | |
| angular.module('Service', ['CustomEvent']) | |
| .factory('Service', ServiceFactory); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment