Ember.js: Where to put common properties?

时间:2015-05-18 17:27:35

标签: ember.js ember-cli

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?

2 个答案:

答案 0 :(得分:1)

我认为服务是您所寻找的。

看看以下内容:

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

以下是我使用服务存储应用程序配置(用户权限/角色,默认值等)的方法:

<强> /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
};

然后我可以在控制器和路由中访问:

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

答案 1 :(得分:0)

你会建立一个服务并在必要的地方注入它。

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

除非你说你有通用的计算属性需要装饰多个不同的模型,在这种情况下你会使用mixin。

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