Is there a good way to manage all events fired on $ rootScope so that you don't end up with events with the same name?

I know events can be very ugly, but is there a good way to manipulate or manipulate string event names so programmers won't be able to give the same string name event when working with different components?

+3


source to share


2 answers


The easiest way to do it: just write your own wrapper that will log events

This is just an example of an idea



angular.service("EventManager", function($rootScope){

  var events_names = {};

  return {
      on: function(name, cb){
        if(!events_names[name]){
            $rootScope.$on(name, cb);
        }
        else{
            console.error("This kind of event already defined!", name);
        }
      }
  }  
});

      

+3


source


There is no angular way. But you can always write a wrapper for $ broadcast / $ emit that has a registry for your event names.



+2


source







All Articles