-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
get access to aurelia container in decorators #57
Comments
yep: import {Container} from 'aurelia-dependency-injection';
// access root container
let container = Container.instance; |
Also, if it can't resolve in the current container it will check the parent until it reaches the root container if I don't remember wrong. |
thx @jdanyow , but i get Error: Cannot read property 'get' of undefined maybe i am doing it wrong
|
When is this code executed? |
this is part of an aurelia plugin it is a decorator to decorate a class inside that plugin |
So the file is actually I guess you have to use the That's what I can think of right now... |
It makes it global here |
@stoffeastrom yeah you are right, the decorator function is imported before bootstrap, in fact it is imported before the configure happens, that is why i tried makeGlobal() before and it i did not work. so still no solution thanks by the way for the prompt reply :) |
What happens if you move the
|
maybe i am wrong, i tried it but it does not work because globlaizeResources works only for custom elements and attributes. my case is that the class i want to decorate is not custom element or attribute. |
Aha.. try |
still no good :( |
thanks a lot man for the effort :) , and i get what you mean, but still it is not what i want, first i dont want to use dynamic system imports, secondly this means we have to do it for every class, and also this still dont fix the decorator function if i am correct!, i really think there should be a better way, i should be able to access the container anywhere and anytime. but again i really appreciate the time you took :) |
Can you post some more code? I just wanted to show that you have to load module with the decorator function in the |
thanks man, first maybe i am wrong. but if you used dynamic system import inside the plugin, it will make path relative to main app, which is not right, unless there is something i have to configure. i see that you added some modifications in the config.js for this to work, but in our case we cant, once someone install the plugin he needs not to modify anything. so another thing is that we gonna have nested usage so importing everything in config function is not practical. let me give you an example of myplugin index.js
myPlugin.js
toolBarItem.js
item1.js
|
thanks again man, but still not working , Container is always undefined :( i noticed you return the decorator as plugin, but really that is not the entry point, the decorator will be applied to classes that myPlugin will load as in my example above, but i tried yours and mine and both did not work |
I don't get it :). It works in my sample. The problem is that you can't load the |
I think you need to recreate in a skeleton-navigation app to be able to understand what you are doing. It feels like this should be possible to do. |
Agree- @devmondo please setup a plunker or skeleton fork so @stoffeastrom can see what you're seeing. |
@stoffeastrom @jdanyow , i get your point, and i can make it work fine like you mentioned, so you example works!, but it breaks as soon as user create items in app it self and pass it to plugin config, because as you said aurelia is not bootstrapped yet.so i either extract the decorator to another repo and init it before plugin start or disable ability for users to add items. |
Guys, there is something really weird, i just nuked jspm folder and installed Aurelia again,and i can get access to Container instance in decorator without any of the suggested solutions above, just plain |
@devmondo |
@EisenbergEffect thanks for the tips, but i really did not change or add anything to my code, just updated aurelia, and it worked. regarding that it might not work, what is your suggestion to a better approach to inject things then in decorators then ? |
Don't use the DI inside a decorator. Decorators are a means of doing metaprogramming and run as part of the definition of the class. So, technically, they should be able to run without any application state. Alternatively, you could use the decorator to store some metadata and then have the DI, as part of the object creation process, locate that metadata and then use it to affect the instance that was created...or else create some other runtime support built on top of the metadata. You could also just create a singleton instance of the EA or other service manually and reference that in your decorator. Then during app startup, you could manually register that in the DI as a singleton so that the same instance was available through that channel. |
we are facing this issue, we are using decorators for example to get instance event aggregater or some shared singleton config class without using inject, but we cant get access to Aurelia container.
we dont want to create new container, because we want to access the resolved instances of the Aurelia main container, unless there is a way for this new container to get access to Aurelia main one?
The text was updated successfully, but these errors were encountered: