Where to store global (app wide) functionallity

Hello!

I’m very new to ember as my last post shows. But I’m getting more and more familiar with it.

We are using Ember-Cli and I’m wondering what is the best way to store functionality which is needed throughout the whole app.

Specifically I have an object which calculates all the permissions in our app. Right now I just do an ES6 import at every file which needs this permission object. But somehow it does not feel super smart. Is it better to use some kind of dependency injection? (I’m not very familiar with Ember dependency injection).

What makes this quite tricky is, that I have to fetch the roles asynchronously from the backend and then store this roles in the permissions object. Right now I initialize this permission object in the application route. I’m also not sure if this is the best place.

Nevertheless this solution is working right now but I think I should refactor it to accomplish a more “Ember”-way of accomplishing this. I’m looking forward to here some design advices from you guys :smile:

Thanks a lot Tschoartschi

Sounds like dependency injection is definitely the way to go. It’s quite easy. Although the Ember guides are outdated (just as the documentation), this article explains it quite well:

http://emberjs.com/guides/understanding-ember/dependency-injection-and-service-lookup/

Mixins are also a nice way to implement app wide functionallity.

Also, check out this article http://www.codepreneur.io/2015/01/25/Using-and-creating-services-with-ember-cli/

Built-in service support is making this kind of thing much simpler

1 Like

You can also just put it in the config/environment.js file

module.exports = function(environment) {
    var ENV = {
    ...
        APP: {
            MY_GLOBAL: 'Hello folks!',
            ...
        }
    };
    ...
};

and import it like this:

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

export default Ember.ObjectController.extend({
    ...
    var hello = config.APP.MY_GLOBAL;
    ...
});

Thanks for the hints and links :slight_smile: I think the service-solution is more what I’m looking for. @kgish: I want to share whole functionallity across my app. So I think storing it on the ENV variable isn’t the best place. What I want to do is something like this (just super quick pseudo-code to get the idea):

if(Permission.allows('actionB', dataX)) {
    //....
}

I’ll read through the article about the service/DI (I didn’t have the time to read it in depth). I’ll come up with questions again if I’m unsure :wink: