发布/订阅设计模式angularjs服务 [英] Pub/Sub design pattern angularjs service

查看:382
本文介绍了发布/订阅设计模式angularjs服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图使用贴在这里马克Rajcok答案

<一个href=\"http://stackoverflow.com/questions/15322971/angular-js-communicate-between-non-dependend-services?answertab=active#tab-top\">angular JS - 非dependend服务之间的通信

我无法理解他的答案。特别这一部分:

  angular.forEach(event1ServiceHandlers,功能(处理){
            处理器(some_data);
});

时的填充功能(这里称之为处理器)的event1ServiceHandlers数组,它是在这个forEach循环触发?

我认为这将是更容易理解一个很好的例子,如何发布/订阅设置。

我有两个服务谁需要交流,但我想避免$ ro​​otScope。$从我读了发布/订阅的服务是最好的办法广播等等。我的一个服务需要我的其他服务执行的功能,但该服务已经有了我的第一个服务作为一个依赖,所以我不能做同样的,因为圆形的依赖两种方式。

我的提问:那么假设你有两个angularjs服务(工厂),如何服务1执行对服务2功能,如果服务2已经有1服务作为一个依赖。不使用$广播和$在


解决方案

  

时的填充功能(这里称之为处理器)的event1ServiceHandlers数组,它是在这个forEach循环触发?



  

如何服务1日服2执行一个功能,如果服务2已经有1服务作为依赖


创建服务3, NotificationService 和以前一样:

  .factory('NotificationService',[功能(){
    变种event1ServiceHandlers = [];
    返回{
        //发布
        event1Happened:功能(some_data){
            angular.forEach(event1ServiceHandlers,功能(处理){
                处理器(some_data);
            });
        },
        //订阅
        onEvent1:功能(处理){
            event1ServiceHandlers.push(处理);
        }
    };
}])

有服务2注册一个回调函数 NotificationService

  .factory('服务2',['NotificationService',
功能(NotificationService){
    // EVENT1处理程序
    VAR的doSomething =功能(someData){
        的console.log('S2'someData);
        //这里做什么
    }
    //订阅EVENT1
    NotificationService.onEvent1(DoSomething的);
    返回{
      //定义公共API的客服2点击这里
    }
}])

每当服务1想要的功能 DoSomething的()对服务2来执行,它可以发布 event1Happened 事件:

  .factory('服务1',['NotificationService',
功能(NotificationService){
    变种someData = ...;
    返回{
       //定义公共API的服务1在这里
       callService2Method:功能(){
         //发布事件
         NotificationService.event1Happened(someData);
       }
    }
}])

I've been trying to use the answer here posted by Mark Rajcok

angular JS - communicate between non-dependend services

I am having trouble understanding his answer. Specially this part:

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

Is the event1ServiceHandlers array populated with functions (here called handler) that is triggered in this forEach loop?

I think it would be much easier to understand with a good example how a publish/subscribe is set up.

I have two services who need to communicate but I want to avoid $rootScope.$broadcast so from what I have read a pub/sub service is the best approach. One of my services need to execute a function on my other service, but that service already has my first service as a dependency so I cannot do the same both ways because of circular dependency.

My question: So assume you have two angularjs services (factory), how does service 1 execute a function on service 2 if service 2 already has service 1 as a dependency. Not using $broadcast and $on

解决方案

Is the event1ServiceHandlers array populated with functions (here called handler) that is triggered in this forEach loop?

Yes

how does service 1 execute a function on service 2 if service 2 already has service 1 as a dependency

Create service 3, NotificationService as before:

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

Have service 2 register a callback function with the 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
    }
}])

Whenever service 1 wants function doSomething() on service 2 to execute, it can publish the event1Happened event:

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

这篇关于发布/订阅设计模式angularjs服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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