简体   繁体   English

存储来自ajax调用的全局变量

[英]Store a global variable from ajax call

I have a list of years in an array format [2018, 2019, 2020] that is returned from my back end. 我有一个从我的后端返回的数组格式的年份列表[2018,2019,2020]。 Right now, i'm using a setup controller to make the ajax call and set it as a value to be used by my ember-power-select. 现在,我正在使用设置控制器进行ajax调用,并将其设置为我的ember-power-select使用的值。 However, I just need this to be done once, like on application load and have it as a global variable, instead of everytime the controller loads since this array will rarely change. 但是,我只需要执行一次,就像在应用程序加载时一样,并将其作为全局变量,而不是每次控制器加载时都这样做,因为此数组很少更改。

How should I go about this ? 我应该怎么做呢?

Forget about globals. 忘记全局变量。 What you need is application-wide availability, which you get by using a services . 您需要的是应用程序范围内的可用性,这是通过使用服务获得的。 A service is created as a singleton (by default) by the application's dependency injection container, and as such, is available anywhere you should choose to inject it. 服务由应用程序的依赖项注入容器创建为单例(默认情况下),因此,在您应该选择注入它的任何位置都可以使用该服务。

Check out this twiddle I put together for you. 看看我为您整理的这个细微旋转 I've made a little mock service that uses a cached value when available. 我做了一个小模拟服务,在可用时使用缓存的值。 To me, this is more straightforward than using a computed property that is promise aware (although there are implementation available). 对我来说,这比使用可承诺感知的计算属性(尽管有实现可用)更直接。

export default Ember.Service.extend({
  loadDates(){
    if(this.dates){
      return Ember.RSVP.resolve(this.dates);
    }else{
      return this._loadAndCacheDatesFromApi(); 
    }
  },
  _loadAndCacheDatesFromApi(){
    // this is actually an api call in your case
    return new Ember.RSVP.Promise((resolve) => {
      setTimeout(() => {
        this.set('dates', ['2019', '2020', '2021']);
        resolve(this.dates);
      }, 5000);
    });
  }
});

We want our loadDates to behave the same whether it's loading from the api or loading from the cache, so we make the function always return a promise (using the resolve function in the cache case). 无论是从api加载还是从缓存加载,我们都希望loadDates的行为相同,因此我们使函数始终返回promise(在缓存情况下使用resolve函数)。 I've used a setTimeout just for illustration...you'd actually make the api call in your case here, and on return, store the dates as a property on the service. 我仅使用setTimeout进行说明...实际上,您需要在此处进行api调用,然后在返回时将dates作为属性存储在服务中。

Now, any route that wants to load this would inject the service (via the Ember dependency injection framework), and then simply make the service call in the setupController 现在,任何想要加载的路由都将注入服务(通过Ember依赖注入框架),然后只需在setupController进行服务调用

import { inject as service } from '@ember/service';
export default Ember.Route.extend({
  dateService: service() // this is shorthand for service('date-service'),
  setupController(controller, model){
    let dateService = this.get('dateService');
    return dateService.loadDates().then((dates) => {
      controller.set('dates', dates);    
    });
}

As you see here, it's opaque to the calling code whether this code hits a cache or is returned from the api. 如您在此处看到的,无论该代码是命中缓存还是从api返回,调用代码都是不透明的。 It's important to note that this works because services are singletons (ie only one instance created on first use). 需要特别注意的是,这是有效的,因为服务是单例的(即,仅在首次使用时创建了一个实例)。 That way, properties that are set are truly shared. 这样,设置的属性才能真正共享。 A refresh to the application would obviously lose the value (which you could store in local storage if you needed more long term storage). 刷新应用程序显然会失去价值(如果需要更多长期存储,可以将其存储在本地存储中)。

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

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