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 to share