EchtFettigerKeks
EchtFettigerKeks

Reputation: 1893

Ember.js: Where to put common properties?

Ive heard about, that controlers gonna get deprecated some day. So im trying to just use components for everything.

But if you'r going to use some calculated properties all over the whole application, where would you implement it?

In general, I would use an application-controller and put in in there.

What is the best practice?

Upvotes: 3

Views: 144

Answers (2)

LukeP
LukeP

Reputation: 10422

I think services are what you are looking for.

Have a look at the following:

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

Here's how I use a service to store application configuration (user permissions/roles, defaults, 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
};

Which I then access in controllers and routes like this:

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

Upvotes: 1

Kingpin2k
Kingpin2k

Reputation: 47367

You would build up a service and inject it everywhere necessary.

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

Unless you are saying you've got common computed properties that need to decorate multiple different models, in that case you would use a mixin.

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

Upvotes: 0

Related Questions