在缓存服务AngularJS承诺对象 [英] Caching a promise object in AngularJS service

查看:119
本文介绍了在缓存服务AngularJS承诺对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现使用承诺AngularJS静态资源的动态加载。问题:我有可能(或不取决于它的显示方式,因此动态)页面上的情侣组件需要从服务器获取静态资源。一旦加载,它可以缓存整个应用程序生命。

我已经实现这个机制,但我新的角度和承诺,我要确保,如果这是一个正确的解决方案\\办法。

  VAR数据= NULL;
VAR deferredLoadData = NULL;功能loadDataPromise(){
  如果(deferredLoadData!== NULL)
    返回deferredLoadData.promise;  deferredLoadData = $ q.defer();  $ http.get(data.json)。然后(功能(RES){
    数据= res.data;
    返回deferredLoadData.resolve();
  },功能(RES){
    返回deferredLoadData.reject();
  });  返回deferredLoadData.promise;
}

所以,只有一个请求时,所有接下来的调用loadDataPromise()返回的第一个应许。这似乎为要求的工作,在已经完成了前一段时间的进展或1。

但它是一个很好的解决方案,以高速缓存承诺?


解决方案

  

这是正确的做法?


是的。在返回的承诺的常用技术功能使用 memoisation ,以避免异步(通常昂贵的)任务的重复执行。该承诺使缓存容易的,因为一个不需要持续和完成操作之间的区别,他们都重新psented作为结果值(下同)承诺$ P $。


  

这是正确的解决方案?


没有。全球数据变量和未定义分辨率不承诺都打算如何工作。相反,履行承诺,结果数据!这也使得编码轻松了不少:

  VAR dataPromise = NULL;函数的getData(){
    如果(dataPromise == NULL)
        dataPromise = $ http.get(data.json)。然后(功能(RES){
           返回res.data;
        });
    返回dataPromise;
}

然后,而不是 loadDataPromise()。然后(函数(){/ *使用全局* /数据})这简直就是的getData( )。然后(功能(数据){...})

要进一步提高模式,您可能希望隐藏 dataPromise 在一个封闭的范围,并注意您将需要为不同的承诺时,的getData 需要一个参数(如URL)。

I want to implement a dynamic loading of a static resource in AngularJS using Promises. The problem: I have couple components on page which might (or not, depends which are displayed, thus dynamic) need to get a static resource from the server. Once loaded, it can be cached for the whole application life.

I have implemented this mechanism, but I'm new to Angular and Promises, and I want to make sure if this is a right solution \ approach.

var data = null;
var deferredLoadData = null;

function loadDataPromise() {
  if (deferredLoadData !== null)
    return deferredLoadData.promise;

  deferredLoadData = $q.defer();

  $http.get("data.json").then(function (res) {
    data = res.data;
    return deferredLoadData.resolve();
  }, function (res) {
    return deferredLoadData.reject();
  });

  return deferredLoadData.promise;
}

So, only one request is made, and all next calls to loadDataPromise() get back the first made promise. It seems to work for request that in the progress or one that already finished some time ago.

But is it a good solution to cache Promises?

解决方案

Is this the right approach?

Yes. The use of memoisation on functions that return promises a common technique to avoid the repeated execution of asynchronous (and usually expensive) tasks. The promise makes the caching easy because one does not need to distinguish between ongoing and finished operations, they're both represented as (the same) promise for the result value.

Is this the right solution?

No. That global data variable and the resolution with undefined is not how promises are intended to work. Instead, fulfill the promise with the result data! It also makes coding a lot easier:

var dataPromise = null;

function getData() {
    if (dataPromise == null)
        dataPromise = $http.get("data.json").then(function (res) {
           return res.data;
        });
    return dataPromise;
}

Then, instead of loadDataPromise().then(function() { /* use global */ data }) it is simply getData().then(function(data) { … }).

To further improve the pattern, you might want to hide dataPromise in a closure scope, and notice that you will need a lookup for different promises when getData takes a parameter (like the url).

这篇关于在缓存服务AngularJS承诺对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆