Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

feat: inject non-annotated tokens as values #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vojtajina
Copy link
Contributor

If a token is an object or a non-annotated function/class and there is no other provider defined for this token, the default provider returns the value instead of calling it.

This is helpful for injecting third-party code (without instantiating it) that is not annotated. Eg. Node's native modules:

var fs = require('fs');

@Inject(fs.readFile)
class Foo {
  constuctor(readFile) {}
}

Fixes #30

BREAKING CHANGE:
This means that every function/class (that should be called/instantiated) has to be annotated with @Inject, even if it takes no arguments.


See how templating and expressionist needs to be refactored:
angular/templating#35
angular/expressionist.js#13

Review on Reviewable

@vojtajina vojtajina added cla: yes and removed cla: no labels May 9, 2014
@Alxandr
Copy link

Alxandr commented May 10, 2014

One problem with your example, though I'm not sure it's something to mitigate, but just something that peoples need to know. If fs.readFile depends on being called as a method and not a function this will go nuclear.

@vojtajina
Copy link
Contributor Author

@Alxandr that's a good point, you would have to inject fs to fix that...

@vojtajina
Copy link
Contributor Author

There is a discussion about this PR at https://groups.google.com/forum/#!topic/angular-dev/sN4cxlqHtKE

Without this feature, we might provide additional module that users would have to load to enable this. Something like https://gist.github.com/vojtajina/0a3a59f28ea9b3b5cef0

@Alxandr
Copy link

Alxandr commented May 10, 2014

@vojtajina or fs.readFile.bind(fs).

If a token is an object or a non-annotated function/class and there is no other provider defined for this token, the default provider returns the value instead of calling it.

This is helpful for injecting third-party code (without instantiating it) that is not annotated. Eg. Node's native modules:
```js
var fs = require('fs');

@Inject(fs.readFile)
class Foo {
  constuctor(readFile) {}
}
```

Fixes angular#30

BREAKING CHANGE:
This means that every function/class (that should be called/instantiated) has to be annotated with `@Inject`, even if it takes no arguments.
@vojtajina vojtajina force-pushed the master branch 2 times, most recently from e46889e to e4c8659 Compare August 23, 2014 16:35
@larsno
Copy link

larsno commented Dec 1, 2014

I must say that I don't thin the injector.bind(fs.readFile).toValue(fs.readFile); is very intuative. If the @Provider syntax will be replaced by the bind syntax then it might make sense. (Part of the confusion is the existing javascript function.bind method).

@dmtrs
Copy link
Contributor

dmtrs commented May 19, 2015

Is there any update on this pr?

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

Successfully merging this pull request may close these issues.

Feature: Inject class without intantiating
4 participants