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

Inject into a helper #11021

Closed
atomkirk opened this issue May 4, 2015 · 6 comments
Closed

Inject into a helper #11021

atomkirk opened this issue May 4, 2015 · 6 comments

Comments

@atomkirk
Copy link

atomkirk commented May 4, 2015

I have this:

export function initialize(container, application) {
    application.inject('component', 'store', 'store:main');
    application.inject('component', 'application', 'application:main');
    application.inject('component', 'applicationController', 'controller:application');
    application.inject('helper', 'applicationController', 'controller:application');
    application.inject('model:device', 'application', 'application:main');
    application.inject('model:room', 'application', 'application:main');
    application.inject('model:setting', 'application', 'application:main');
    application.inject('route:application', 'application', 'application:main');
    application.inject('transform', 'application', 'application:main');
}

export default {
  name: 'injections',
  initialize: initialize
};

notice:

application.inject('helper', 'applicationController', 'controller:application');

but in the helper, this.get('applicationController') is not defined:

import Ember from 'ember';
import environment from '../utils/environment';

export function staticImg(path, options) {
    if (path) {
        var staticRoot = this.get('applicationController').get('model.staticRoot');
        return new Ember.Handlebars.SafeString('<img src="' + environment.staticHost() + staticRoot + path + '" class="' + ( options.hash.classNames || "" ) + '">');
    }
    else {
        return "";
    }
}

export default Ember.Handlebars.makeBoundHelper(staticImg);

In the docs it says you can inject onto helpers, and this exact setup working on other framework classes but not here…

@rwjblue
Copy link
Member

rwjblue commented May 4, 2015

You cannot inject into helpers. I believe that the docs have been updated, but please point me to where you noticed that it mentioned this was possible so we can fix the doc...

@atomkirk
Copy link
Author

atomkirk commented May 4, 2015

Ah crap, you did update them for 1.11. Thanks. Ok, then what is the direction we should go if we need access to container stuff from within helpers and especially plain Ember.Object instances (which have no container property)?

@mmun
Copy link
Member

mmun commented May 5, 2015

@atomkirk Instead of trying to inject something into a helper, you should inject into a controller/component and pass the injected object into the helper (or just use a computed property). Or just use a component instead of a helper.

@devlo
Copy link

devlo commented Jun 30, 2015

What if i am using helper inside component and i need to access user data too that i normally inject to routes and controllers ? It's not accessible inside that helper.
Passing it to every component seems not efficient, i would need to make for example user.name for every component, while it's the same for all of them. So if i have 50 components i would need to pass the same value to 50 components to access it from helper inside component...
Any way to resolve this ?

@mixonic
Copy link
Sponsor Member

mixonic commented Jun 30, 2015

@rwjblue
Copy link
Member

rwjblue commented Dec 4, 2015

For anyone coming across this later, helpers in Ember 2.x are now "real" objects and can have access to services (via Ember.inject.service or initializer based injections).

Example:

export default Ember.Helper.extend({
  i18n: Ember.inject.service('i18n'),

  compute(params, hash) {
    let i18n = this.get('i18n');
    // stuff here
  }
});

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