Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component Controller - Inject new service require change in 3 place #85

Open
phucpnt opened this issue Jun 27, 2016 · 7 comments
Open

Comments

@phucpnt
Copy link

phucpnt commented Jun 27, 2016

image

Hello,

Whenever you inject a new service, you need to change 3 place: 1) register the server 2) add new params into the constructor 3) assign the service into the component this. Please check the image I attach. Should we simplify those requirements?

@d3lm
Copy link

d3lm commented Jun 27, 2016

@phucpnt First thing you can do is to remove the assignment of your Service to a variable in constructor. If you declare your Service parameter as private or public can can omit the assignment, because this will happen automatically when you add a modifier to your parameter. At least thats what I do. Another thing is that you could use a task which runs ngAnotate, which I would highly recommend. When using ngAnnotate you can also omit the $inject statemenet. However, you will need to add ngInject into your constructor. The downside to this is that you must use the name of the Service that was used to register the service. So you cannot e.g. write ts (for TodoService). At the end you only have one place where the Service is being typed out. Later on, if you change the name or anything else you could find and replace all occurrences of your service within a file or what ever it is you wanna do then. Personally, I am not 100% sure what is best - using the $inject or a build step and omitting this. Happy to discuss this with other people.

@phucpnt
Copy link
Author

phucpnt commented Jul 3, 2016

@d3lm thanks for your comments. I'm still thinking about how to make the connection between the different services more easily to grasp by. I has a few idea regards the dependency injection but those not connected well and give me clearer picture till now... I will keep you posted.

@floriangosse
Copy link
Contributor

floriangosse commented Jul 4, 2016

@phucpnt I wrote a small function which automatically bind the injected dependencies to this. And if you use ngAnnotate you only need to define your dependencies as parameters of your constructor.

Check out the class wrapper:

function binder(constructor) {
   function wrapConstructor () {
      var args = Array.prototype.slice.call(arguments);

      constructor.$inject.reduce((self, depName, depIndex) => {
         self[depName] = args[depIndex];

         return self;
      }, this);

      return constructor.apply(this, args);
   }
   wrapConstructor.prototype = constructor.prototype;
   wrapConstructor.$inject = constructor.$inject;
   return wrapConstructor;
}

// Usage:
//    module.exports = binder(YourController);

Note: I didn't test it with angular. I adapted the instantiation of angular for testing.

@AkosLukacs
Copy link

@floriangosse Looks good, as long as you don't minify your code. But I guess ngAnnote can solve that.
A sidenote: drawback of class decorators (those could be used to wire up the controller and dependency injection) is that the class name, and constructor parameters are not emitted as metadata. Therefore lost at minification, if you mangle the parameter and class names during minification.

@caioiglesias
Copy link
Contributor

caioiglesias commented Jul 26, 2016

If you declare your Service parameter as private or public can can omit the assignment

@d3lm how exactly? Typescript?

@floriangosse
Copy link
Contributor

@AkosLukacs Sure, if you minify your code without ngAnnotate it will break but application. But you should use strictDi and ngAnnotate.

@d3lm
Copy link

d3lm commented Aug 7, 2016

@caioiglesias Yes, I was refering to TypeScript. The constructor would look like this:

constructor(private FooService) { }

Then FooService is then available by accessing this.FooService in your class anywhere you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants