简体   繁体   English

Ember.js-在ember中存储应用程序特定数据的最佳方法是什么?

[英]Ember.js - What is the best way to store application specific data in ember?

I need to access the application specific data in my components as well as routes. 我需要访问组件中的特定于应用程序的数据以及路由。 I also need to set the application specific data from normal JS. 我还需要从普通JS设置应用程序特定的数据。

I have currently created an object with global namespaces (App.globalSetting) and then created the variables as properties on this object. 我目前已使用全局名称空间(App.globalSetting)创建了一个对象,然后在该对象上将变量创建为属性。

I am then able to set and get the variable using App.globalSetting.set() and App.globalSetting.get(). 然后,我可以使用App.globalSetting.set()和App.globalSetting.get()来获取变量。

Is the above method a good practice or is there a better way to do the same. 以上方法是好的做法还是有更好的方法做到这一点?

Also the data to be stored is critical. 同样,要存储的数据也很关键。 So please suggest a best way to accomplish this task. 因此,请提出一种完成此任务的最佳方法。

You may want to take a look at Services: http://guides.emberjs.com/v2.0.0/services/ . 您可能需要看一下服务: http : //guides.emberjs.com/v2.0.0/services/

From the guides: 从指南:

"An Ember.Service is a long-lived Ember object that can be injected as needed." “ Ember.Service是寿命很长的Ember对象,可以根据需要注入它。”

Example service: 服务示例:

settings-service.js 设置- service.js

export default Ember.Service.extend({
  exampleSetting: true,
  update(value) {
    this.set('exampleSetting', value);
  }
});

How to access this service from a Component (or Route): 如何从组件(或路由)访问此服务:

export default Ember.Component.extend({
  settings: Ember.inject.service('settings-service'),
  actions: {
    doSomething(val) {
      this.get('settings').update(val);
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM