Skip to content

Instantly share code, notes, and snippets.

@sukima
Last active August 16, 2016 16:29
Show Gist options
  • Save sukima/d51c4dafbc34122d45e12d8223acf4a2 to your computer and use it in GitHub Desktop.
Save sukima/d51c4dafbc34122d45e12d8223acf4a2 to your computer and use it in GitHub Desktop.
Dependency Injection
import Ember from 'ember';
import Example from '../utils/example';
export default Ember.Controller.extend({
myService: Ember.inject.service(),
test1: Ember.computed({
get() {
// This is hard to maintain across the app but
// easy for developers to understand.
const myService = this.get('myService');
return Example.create({myService});
}
}),
test2: Ember.computed({
get() {
// This is the how Ember handles it but is a
// bit of a departure from typical Ember docs
const owner = Ember.getOwner(this);
const ExampleFactory = owner._lookupFactory('util:example');
return ExampleFactory.create();
}
})
});
import Ember from 'ember';
export default Ember.Service.extend({
foo: 'bar'
});
<h1>Test 1</h1>
<p><code>test1.foo == "{{test1.foo}}"</code></p>
<h1>Test 2</h1>
<p><code>test2.foo == "{{test2.foo}}"</code></p>
{
"version": "0.10.4",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.7.0",
"ember-data": "2.7.0",
"ember-template-compiler": "2.7.0"
},
"addons": {}
}
import Ember from 'ember';
export default Ember.Object.extend({
myService: Ember.inject.service(),
foo: Ember.computed.alias('myService.foo')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment