Ember.js: Where to Place Shared Properties?

I have heard that these controllers will be deprecated one day. So I'm trying to just use components for everything.

But if you are going to use some calculated properties throughout your application, where do you implement it?

In general, I would use a controller app and inject there.

What's the best practice?

+3


source to share


2 answers


I think services are what you are looking for.

Take a look at the following:

http://www.hutchinson.io/ember-services-and-dependency-injection/

Here I am using a service to store application configuration (user permissions / roles, default values, etc.):

/app/services/configuration.js



import Ember from 'ember';

export default Ember.Service.extend({
    roles: ['Administrator', 'Manager', 'User', 'View-only'],
    // ...
});

      

/app/initializers/configuration-service.js

export function initialize(container, application) {
    application.inject('route', 'configuration', 'service:configuration');
    application.inject('controller', 'configuration', 'service:configuration');
}

export default {
    name: 'configuration-service',
    initialize: initialize
};

      

What I then get in controllers and routes like this:

export default Ember.Controller.extend({
    allRoles: function() {
        return this.configuration.roles;
}).property();

      

+1


source


You would create a service and deploy it wherever you need it.

http://discuss.emberjs.com/t/services-a-rumination-on-introducing-a-new-role-into-the-ember-programming-model/4947



Unless you say that you have general computational properties that need to be decorated with several different models, then you will be using a mixin.

http://emberjs.com/api/classes/Ember.Mixin.html

0


source