What's the correct place to inject / load a service containing self-registering components?

I am working on an AngularJS application that has some self-registering components. Specifically, there are some services that do not directly offer any public interface; they just register some internal objects in some directory provided by another service.

You can imagine the body of such a factory service like this:

var internalThing = {
    // members ...
};
thingRegistryService.registerThing(internalThing);

return {};

      

This way I only need to ensure that the service is loaded at some point.

The dilemma I am facing is this: Since the service does not provide any public functions and just needs to "be there", there is no reason to inject it anywhere. Since it is not entered, it is never created. Because it is never created, the components are never registered with the service.

I can inject the service as normal when in some service or controller that I know will be loaded - but then I basically leave the unused argument in the code (which, if it is the last argument in the list, would even be described as error based on project JSHint settings).

Alternatively, I can self-register in a method in the service and call that wherever I inject the service. This would make service injection "useful", but in turn I would have to deal with multiple calls myself, rather than relying on AngularJS's built-in Singleton service.

Or ... should I go another route by providing a method like registerThing

somewhere that takes the name of the service as a string and that would be internally simple to call $injector.get

? Of course, this again repeats the question of where the right place for such a challenge would be.


Small Background: This is part of a larger project developed by a large team. The Build and / or Deployment wizards somehow handle that any JavaScript code file handed to our VCS by any developer will be available for AngularJS dependency injection. So any JavaScript that needs to be added has to be provided as some kind of AnglerJS service, controller, etc.


What is the correct way to download such self-registration?

+3


source to share


2 answers


The block is in the correct place to initialize your module angular.module('yourModule').run

.

In the case of "self-registration", I think it's better to have an implicit method for this:



angular.module('yourModule').run(['service'], function (service) {
  service.init();
})

      

0


source


If your build system magically provides all the JS code, is import necessary?

The self-registration architecture, as your build system suggests, does not require import. JSHint import errors are the cost of this magic.



I used similar behavior with a namespace-like design. It can be used for self-registration, although the import becomes complex and does not work with ES6 modules.

This is close to my point: http://blog.assaf.co/automating-component-registration-in-angularjs/

0


source







All Articles