在我的服务,我厂查找了大量的数据集 - 我要坚持它,并检查其存在,以避免再次调用它 [英] In my service-factory I lookup up a large dataset - I want to persist it and check for its existence to avoid calling it again

查看:92
本文介绍了在我的服务,我厂查找了大量的数据集 - 我要坚持它,并检查其存在,以避免再次调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务(厂)进行API调用和响应数据分配给一个变​​量:

My service (factory) makes an API call and assigns response data to a variable:

    .factory('MedicationMatchingNamesFactory', 
                ['$http', '$q', 'MedicationDisplayNamesFactory', 
                    function MedicationMatchingNamesFactory($http, $q, MedicationDisplayNamesFactory){

        return {
            getMatchingNames: function(inputValue){

                var matches = [];

                // I thought this may be set as null so I'm able to 
                // differentiate it from the below array form (when exists)
                var allNames = null;

                    MedicationDisplayNamesFactory.getDisplayNames().then(
                            function(response){

                                // this is the large dataset - now as array
                                var allNames = response.data.displayTermsList.term;

                                angular.forEach(allNames, function(value, key){

                                    if(value.indexOf( inputValue ) !== -1){
                                        matches.push(value);
                                    }

                                });
                            }
                        );


                return matches;
            }
        };
        return MedicationMatchingNamesFactory;
    }])

我采用了棱角分明的用户界面的用户界面,选择指令的基础上输入的字符串这个大数据集内进行搜索。

I'm using Angular-UI's "ui-select" directive to search within this large dataset based on entered string.

在我的控制器:

        $scope.$on('inputStarted', function(event, value){
            if(value.length > 3){
                $scope.meds.displayNames = MedicationMatchingNamesFactory.getMatchingNames(value);
                console.log($scope.meds.displayNames);
            }
        });

现在,为了避免查询API(实际上是调用包含在调用API其他服务),每一次的输入字符数大于3,我认为这将是巨大的,如果我能够检查是否 allNames (空,就调用API)或它是一个阵列 (跳过调用,只需使用)。

Now, to avoid querying the API (actually call another service containing the call to API) every time the number of input characters is greater than 3, I think it would be great if I'm able to check whether allNames is null (empty, do call API) or it's an array (skip the call, just use that).

我试过移动外呼的 angular.forEach 的一部分,承诺不过呢,很明显,什么都不会发生,因为它不是在第一次运行尚未解决。

I tried moving the angular.forEach part outside the call and promise but then, obviously, nothing happens because it's not resolved yet on the first run.

有没有办法让这个 allNames 数据核对的的我做的API调用?

Is there a way to have this allNames dataset checked before I do the API call?

推荐答案

其实解决这个问题很简单:我只是去了的sessionStorage 这似乎契合完美我在这种情况下,需要为我所需要的大数据集只持续了本次会议,如果它失去了对下一个也没关系。目标是prevent读取多于一次,也就是说,超过必要的,因为这些值内不太可能在该会话期间被改变。

Actually the solution to this problem is very simple: I just went with the sessionStorage which seems to fit perfectly my needs in this case as I need the big dataset persisted just for this session and it doesn't matter if it's lost on the next one. The goal is to prevent fetching it more than once, that is, more than necessary, as the values inside are not likely to be changed during that session.

这篇关于在我的服务,我厂查找了大量的数据集 - 我要坚持它,并检查其存在,以避免再次调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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