在AngularJs在清除服务队在注销数据的最佳做法 [英] Best practices for Clearing data in sevices on logout in AngularJs

查看:117
本文介绍了在AngularJs在清除服务队在注销数据的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用互联网服务和缓存大量的results.By缓存我在对服务的变量意味着存储的多种服务​​。当用户注销数据应该被清除。
这些服务如下所示(简体版):

I have several services which uses a webservice and caches a lot of the results.By caching i mean storing in a variable on the service. When a user logs out the data should be cleared. The services looks like the following (simplified version):

class DataService   {

  private data;

  constructor($http)
  {
     $http.get(url).then((response) => 
       { 
          this.data = response.data;
       });
  }

}

这是打字稿,但解析为这样的事情:

Which is typescript, but resolves to something like this:

var DataService = (function () {
    function DataService($http) {
        var _this = this;
        $http.get(url).then(function (response) {
            _this.data = response.data;
        });
    }
    return DataService;
})();

我可以明确使用该课题
这是做这样的事情:

I could clear the data using the answer in This Question Which is doing something like this:

$rootScope.on('logout',function(){
  this.data = [];
});

然而,这是一个很大$ C $的c当我们有多个服务和控制器。我们都知道这个新家伙将一些新数据添加到服务,他忘记将它添加到注销序列。这只不过是一个不好的做法。

However, this is a lot of code when we have multiple services and controllers. And we all know that this new guy will add some new data to a service and he forgets to add it to the logout sequence. It is simply a bad practice.

相若,数据被存储在$范围在应用程序的各部分,这必须被清除为好。
范围是相当简单的,作​​为控制器构造上装载每个页面的访问,然后将覆盖数据。

Similarily, data are stored on $scope in various parts of the application and this must be cleared as well. The scope is rather simple as constructors for the controllers are loaded on each page visits and will then override the data.

一个propossed溶液是使一个刷新,但是这给可怕的用户体验。

One propossed solution is to make a refresh, however this gives horrible user experiences.

的一个解决方案可以是使角相信从未被创建的服务或重新加载角完全。

One solution could be to make angular believe that the services have never been created or reload angular completely.

什么是做到这一点的最好方法是什么?它是坏的变量数据存储在服务?

What is the best way to do this? Is it bad to store data in variables on services?

推荐答案

您可以创建一个服务,这是负责清除所有来自其他服务的数据。在这种情况下,你只需要调用明确这项服务的方法,并实现个人清楚这一个(所以只有实现一次)内部调用。

You can create a service which is responsible clearing all the the data from the other services. In this case you only have to call the "clear" method of this service, and implement the individual clear calls inside of this one (so only implemented once).

这让你还,你有哪些需要被清除所有的时间服务的数据,因为你可能在未来例外的概述优势。

This gives you also the advantage that you have an overview of which services data need to be cleared all the time, since you might have exceptions in the future.

也看到这个答案,供大家参考:<一href=\"http://stackoverflow.com/questions/21004760/injecting-a-service-into-another-service-in-angularjs\">Injecting一个服务到另一个服务angularJS

Also see this answer for your reference: Injecting a service into another service in angularJS

这篇关于在AngularJs在清除服务队在注销数据的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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