-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Guide to Object management and dependency injection in Ember #1293
Conversation
although the container exists, for the most part it should be considered an implementation detail. The public api is actually only
I would prefer for today that these are the API's we highlight It is likely fine, to show people how the container does the magic, but I would like to convey a clear message, |
```js | ||
App.IndexView = Ember.View.extend({ | ||
router: function(){ | ||
return this.container.lookup('router:main'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
container.lookup
exist to enable escape hatches, for ember API deficiencies. In lots of cases, and initializer with an injection rule is much superior. This is because it also performs validation as the object is instantiated, provided quick-fail feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, But I would need to add documentation on initializers for that right? And mention the correct usage for lookup?
Ember.Application.initializer({
name: "store",
initialize: function(container, application) {
container.register('store:main', application.Store);
container.injection('controller', 'store', 'store.main');
}
});
Like this? ^
Shall I add an FAQ similar to the run loop page where I put up info about |
@@ -369,6 +369,8 @@ Understanding Ember.js: | |||
url: "understanding-ember/debugging" | |||
- title: "The Run Loop" | |||
url: "understanding-ember/run-loop" | |||
- title: "Object Management and Dependency Injection" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might say "Service Lookup and Dependency Injection", though it is more technical than "Object Management". "Object Management" just strikes me as a vague title.
@pksjce So though my talk dived deep into how the container powers Ember internals, there is a core team consensus that the API is not extremely stable. Because of this, they would like to steer people away from using the container API directly. For a website guide, it is probably best to give the specifics of What this means is that a guide section for Service Lookup and Dependency Injection might better focus on these container-powered APIs:
I think approaching the guide with a pattern focus instead of an instruction focus may be ideal.
Thanks for the pass at this! Let me know if you want more help on it to move it forward. |
I can do that. Let me have another go at this. I'll notify you on my next PR soon enough. |
can you please re-use this existing PR, it will make it much easier to track the comments and improvements. |
Hmm, |
@lukemelia It's use should not be encouraged, as typically it results in more brittle failures, then it's injection based counter-parts. |
Let's drive this to completion |
@mixonic - I tried to stick to what you had advised. Have documented needs, inject/register and initializers. I'm a bit unsure of the usage of DI in testing. Can you have a look at this http://emberjs.jsbin.com/qafat/1/edit?html,js,console,output |
@pksjce will review. Thanks for taking another pass!! fwiw I've improved the documentation for |
Rewrite some early sections of the dependency injection guide
@mixonic - Thanks very much for the edit! Inline examples while explaining theory was good idea. So, do you suggest removing explanations on Application initializers? |
@pksjce I think using initializers for the |
##### Simple Dependency Injection with needs | ||
|
||
A common use-case for dependency injection is that of a singleton service. For | ||
instance, a controller maintaining the session state may exposed to many other |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be exposed
Okay. I made the corrections. |
Revamp the audio demo and Crosslink docs from controllers to DI guide
Hey. Any update? |
Hoping we can get this merged in soon! |
}); | ||
``` | ||
|
||
Now, to register it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this must occur in an initializer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stefanpenner - That means something like
Ember.Application.initializer({
initialize: function(container, application) {
container.register('logger:main' ...
}
});
This documentation does not have information about using container
object right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use:
Ember.Application.initializer({
initialize: function(container, application){
application.register('logger:main', /* ...whathaveyou... */);
}
});
application
has .register
and .inject
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping
Touched on some of @JulianLeviston's feedback in pksjce#3, but the last section still seems to need some polish. Will find more time to edit/write this week. |
Some further edits via @JulianLeviston
@pksjce I'm going to fork this into a new pull request, since I presume you don't have further interest in pushing it forward. I think it will save us both a bunch of time. |
Forked to #1581 |
Closing in favor of #1581. |
I saw @mixonic s talk at http://www.youtube.com/watch?v=iCZUKFNXA0k#t=6318
This is how I understood the Ember container to be and thought new people trying to look inside Ember should know it too.