【问题标题】:Pub/Sub design pattern angularjs service发布/订阅设计模式 angularjs 服务
【发布时间】:2016-02-09 13:18:16
【问题描述】:

我一直在尝试使用 Mark Rajcok 在此处发布的答案

angular JS - communicate between non-dependend services

我无法理解他的回答。特别是这部分:

angular.forEach(event1ServiceHandlers, function(handler) {
            handler(some_data);
});

event1ServiceHandlers 数组是否填充了在此 forEach 循环中触发的函数(此处称为处理程序)?

我认为通过一个很好的例子来理解如何设置发布/订阅会更容易。

我有两个需要通信的服务,但我想避免使用 $rootScope.$broadcast 所以从我所读到的 pub/sub 服务是最好的方法。我的一个服务需要在我的另一个服务上执行一个功能,但是该服务已经将我的第一个服务作为依赖项,因此由于循环依赖,我无法同时执行两种方式。

我的问题:假设您有两个 angularjs 服务(工厂),如果服务 2 已经将服务 1 作为依赖项,服务 1 如何在服务 2 上执行功能。不使用 $broadcast 和 $on

【问题讨论】:

    标签: javascript angularjs design-patterns publish-subscribe


    【解决方案1】:

    event1ServiceHandlers 数组是否填充了在此 forEach 循环中触发的函数(此处称为处理程序)?

    是的

    如果服务 2 已经将服务 1 作为依赖项,那么服务 1 如何在服务 2 上执行功能

    像以前一样创建服务 3,NotificationService

    .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);
            }
        };
    }])
    

    让服务 2 向 NotificationService 注册一个回调函数:

    .factory('Service2', ['NotificationService',
    function(NotificationService) {
        // event1 handler
        var doSomething = function(someData) {
            console.log('S2', someData);
            // do something here
        }
        // subscribe to event1
        NotificationService.onEvent1(doSomething);
        return {
          // define public API for Service2 here
        }
    }])
    

    每当服务 1 想要服务 2 上的函数 doSomething() 执行时,它可以发布 event1Happened 事件:

    .factory('Service1', ['NotificationService',
    function(NotificationService) {
        var someData = ...;
        return {
           // define public API for Service1 here
           callService2Method: function() {
             // publish event
             NotificationService.event1Happened(someData);
           }
        }
    }])
    

    【讨论】:

    • 谢谢! (我删除了您希望删除的评论)。从未想过您可以将函数存储在数组中并像这样循环并执行它们。使用第三个服务订阅也花了一些时间让我头晕目眩:)
    • @AshkanAldini,是的,设计模式有点难以理解。我稍微修改了示例:我将服务 2 函数重命名为 doSomething()。希望这可能会让未来的读者更容易理解。
    【解决方案2】:

    在他的示例中,NotificationService 是任何现有服务都依赖的新服务。他提供了Service1 的实现,但Service2 本质上是相同的......两者都依赖于NotificationService,并且彼此都不知道。

    Service1Service2 分别通过调用NotificationService.onEvent1(event1Happened); 订阅事件并通过调用NotificationService.event1Happened(my_data); 触发事件。

    【讨论】:

    • 这部分我理解,但我想提供一个实际使用的例子,如何使用 NotificationService 在服务之间设置订阅
    • 真的没有比这更多的了……假设Service1只负责触发一个事件,Service2只负责处理一个事件。然后 Service2 将通过调用 NotificationService.onEvent1(event1Happened); 通过通知服务订阅,Service1 将通过调用 NotificationService.event1Happened(my_data); 发布事件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 2019-07-23
    • 2021-05-06
    相关资源
    最近更新 更多