简体   繁体   English

Angular中的在线/离线应用程序

[英]Online/Offline App in Angular

I am developing an application in Angular that makes many calls to a couple of webservices. 我正在Angular中开发一个应用程序,它可以调用几个Web服务。 I want to develop an offline component to the app such that it will cache certain webservice results to LocalStorage and use them when the connection is offline. 我想为应用程序开发一个离线组件,以便它将某些Web服务结果缓存到LocalStorage并在连接脱机时使用它们。

That part is fairly simple, and the part I am having the most trouble with is how to branch logically when the app is offline. 这部分相当简单,我遇到的最麻烦的部分是如何在应用程序离线时进行逻辑分支。

Here is my current flow: 这是我目前的流程:

  1. User loads the page 用户加载页面
  2. Webservice calls happen as usual Web服务调用照常发生
  3. $http interceptor looks for a 404 error and marks a $rootScope.isOnline boolean flag to indicate we are offline, otherwise if no 404 then we mark as online $ http拦截器查找404错误并标记$rootScope.isOnline布尔标志以指示我们处于脱机状态,否则如果没有404则标记为在线

I want my code to branch depending on this flag, in a way that is maintainable. 我希望我的代码以可维护的方式依赖于此标志进行分支。 As such, I was looking at using dependency injection to inject either an 'online' service which makes calls to the webservice, or an 'offline' service, which interfaces with the LocalStorage results if they exist. 因此,我正在考虑使用依赖注入来注入“在线”服务,该服务调用Web服务,或者注入“离线”服务,如果存在,则与LocalStorage结果进行交互。

Can I base my dependency injection based off the online/offline flag to inject the correct service like so? 我可以根据在线/离线标志来建立依赖注入,以便像这样注入正确的服务吗?

.factory('AuthService', ['$rootScope', '$injector', function($rootScope, $injector) {
    if($rootScope.isOnline) {
        return $injector.get('OnlineAuthService');
    }
    else {
        return $injector.get('OfflineAuthService');
    }
}])

.service('OnlineAuthService', ['$rootScope', '$http', '$location', 'serviceEndpoint', 'securityEndpoint', 'organisationId', function ($rootScope, $http, $location, serviceEndpoint, securityEndpoint, organisationId) {
    this.ensureSession = function (data) {
        // Do some connection to the webservice
    };
}])

.service('OfflineAuthService', ['$rootScope', function ($rootScope) {
    this.ensureSession = function (data) {
        // Do some LocalStorage stuff
    };
}])

AuthService.ensureSession(data);

The issue I am having is that $rootScope.isOnline is not marked as offline before the first call to my webservice, so even when the connection is offline, the dependency injection looks at $rootScope.isOnline and injects the OnlineAuthService. 我遇到的问题是$rootScope.isOnline在第一次调用我的webservice之前没有被标记为脱机,因此即使连接处于脱机状态,依赖注入也会查看$rootScope.isOnline并注入OnlineAuthService。

Is this the correct way of developing an Online/Offline app in Angular or is there a better way? 这是在Angular中开发在线/离线应用程序的正确方法还是有更好的方法?

There are many ways to do this. 有很多方法可以做到这一点。 Take a look at this simplified plunker: 看看这个简化的plunker:

http://plnkr.co/edit/CpiJaF480Mai050b63RC?p=preview http://plnkr.co/edit/CpiJaF480Mai050b63RC?p=preview

The idea there is that you have the singleton service object returned by the service, but then you copy over different function methods as online/offline changes. 这个想法是你有服务返回的单件服务对象,但是你可以将不同的函数方法复制为在线/离线更改。

There are many other ways to do this, such as adding levels of indirection (creating a service from which you can request the current active service for example). 还有许多其他方法可以做到这一点,例如添加间接级别(例如,创建一个可以请求当前活动服务的服务)。

You can not simply do it with angular injection, because both factory and service calls will get called once and only once and will simply register a single instance for the service name (they will not get called every time you inject). 您不能简单地使用角度注入来执行此操作,因为工厂和服务调用都将被调用一次,并且只会为服务名称注册一个实例(每次注入时都不会调用它们)。

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

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