【问题标题】:How to use deferred object to store result and serve it from local memory next time?下次如何使用延迟对象存储结果并从本地内存中提供结果?
【发布时间】:2019-06-14 21:48:15
【问题描述】:

我想从服务器获取小胡子模板并将其存储在本地以供以后使用。

我正在使用 $.get 在我的代码中具有类似承诺的行为,但是如何在第一次调用之后存储结果以供以后使用并使用这个存储的代码而不是另一个服务器调用?

我现在拥有的:

...

getTemplate() {
    return $.get(this.templateUrl);
}

...

this.getTemplate().then((template) => {}

【问题讨论】:

    标签: javascript ecmascript-6 jquery-deferred


    【解决方案1】:

    将其存储在局部变量中?:

    getTemplate() {
      if(this._cached)
        return this._cached;
    
      return this._cached = $.get(this.templateUrl);
    }
    

    其余的应该通过浏览器的caching来完成

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 2013-07-13
      • 2016-03-30
      相关资源
      最近更新 更多