角JS - 非dependend服务之间的通信, [英] angular JS - communicate between non-dependend services

查看:154
本文介绍了角JS - 非dependend服务之间的通信,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在新的角度和遇到一个左右为难:

I am new in angular and encounter a catch-22:

事实:


  1. 我有我记录的东西(我的记录器)的服务。

  1. I have a service that logs my stuff (my-logger).

我已经更换了$的ExceptionHandler(角),用我自己的实现,它捕获的异常转发给我的记录器服务

I have replaced the $ExceptionHandler (of angular), with my own implementation which forwards uncaught exceptions to my-logger service

我有其他的服务,推进服务,需要得到通知,每当一个致命的消息要在使用我的采集器我的应用程序的地方记录下来。

I have another service, pusher-service, that needs to be notified whenever a fatal message is to be logged somewhere in my application using 'my-logger'.

问题:

我不能有'我的记录器'将取决于'推',因为它会创建循环依赖关系(如'推'使用$ HTTP的圆圈:$的ExceptionHandler - >我的记录器 - >推 - > $ HTTP - > $的ExceptionHandler ...)

I can't have 'my-logger' be depend on 'pusher' since it will create circular dependency (as 'pusher' uses $http. The circle: $ExceptionHandler -> my-logger -> pusher -> $http -> $ExceptionHandler...)

我尝试:

为了使这些服务2与对方沟通,我想用$观看推进服务:手表在$ rootscope将在我的采集器进行更新的属性。
但是,试图消耗$ rootScope在我的记录器,以更新其推手表,我不能在循环依赖的财产,因为它证明,$ rootscope依赖于$的ExceptionHandler(圆时:$的ExceptionHandler - >我的记录器 - > $ rootScope - > $的ExceptionHandler)

In order to make these 2 services communicate with each other, I wanted to use $watch on the pusher-service: watches a property on $rootscope that will be updated in my-logger. But, when trying to consume $rootScope in 'my-logger', in order to update the property on which the 'pusher' "watches", I fail on circular dependency as it turns out that $rootscope depends on $ExceptionHandler (the circle: $ExceptionHandler -> my-logger -> $rootScope -> $ExceptionHandler).

试图找到一个选项来获得,在运行时,范围对象,在其上下文我的记录器服务工程。不能找到这样的选项。

Tried to find an option to get, at runtime, the scope object that in its context 'my-logger' service works. can't find such an option.

不能使用广播为好,因为它要求我的采集器以访问范围($ rootScope),这是不可能的,因为上面所看到的。

Can't use broadcast as well, as it requires my-logger to get access to the scope ($rootScope) and that is impossible as seen above.

我的问题:

有没有办法有两个服务,通过第三方的实体沟通的角度的方式?

Is there an angular way to have two services communicate through a 3rd party entity ?

任何想法,这可怎么解决呢?

Any idea how this can be solved ?

推荐答案

使用充当通知/发布订阅服务服3日:

Use a 3rd service that acts as a notification/pubsub service:

.factory('NotificationService', [function() {
    var event1ServiceHandlers = [];
    return {
        // publish
        event1Happened: function(some_data) {
            angular.forEach(event1ServiceHandlers, function(handler) {
                handler(some_data);
            });
        },
        // subscribe
        onEvent1: function(handler) {
            event1ServiceHandlers.push(handler);
        }
    };
}])

以上,我只显示一个事件/消息类型。每增加一个事件/消息将需要自己的阵列,发布方式和subscribe方法。

Above, I only show one event/message type. Each additional event/message would need its own array, publish method, and subscribe method.

.factory('Service1', ['NotificationService',
function(NotificationService) {
    // event1 handler
    var event1Happened = function(some_data) {
        console.log('S1', some_data);
        // do something here
    }
    // subscribe to event1
    NotificationService.onEvent1(event1Happened);
    return {
        someMethod: function() {
           ...
           // publish event1
           NotificationService.event1Happened(my_data);
        },
    };
}])

客服2将是codeD类似服务1。

Service2 would be coded similarly to Service1.

注意如何$ rootScope,$广播和范围不使用这种方法使用,因为与服务间的通信是不需​​要的。

Notice how $rootScope, $broadcast, and scopes are not used with this approach, because they are not needed with inter-service communication.

通过上面的实现,服务(一旦创建)保持认购的应用程序的生命。你可以添加方法来处理退订。

With the above implementation, services (once created) stay subscribed for the life of the app. You could add methods to handle unsubscribing.

在我当前的项目,我用同样的NotificationService也处理PubSub进行控制范围。 (请参阅Updating "前段时间"在Angularjs和Momentjs 如果有兴趣)值

In my current project, I use the same NotificationService to also handle pubsub for controller scopes. (See Updating "time ago" values in Angularjs and Momentjs if interested).

这篇关于角JS - 非dependend服务之间的通信,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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