避免与AngularJS Mediator模式循环依赖 [英] Avoiding circular dependencies with AngularJS Mediator pattern

查看:286
本文介绍了避免与AngularJS Mediator模式循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个服务和调解人。如果我想调解员打电话,双方的服务调用,我想我需要让他们彼此依赖。这将导致循环依赖错误。

I have two services and a mediator. If I want the mediator to call AND be called by both services, I think that I need to make them dependencies of each other. This causes a circular dependency error.

取决于中保调用中保方法

中保取决于调用方法

时使用的事件或承诺唯一的解决办法?我是否正确地实现这种模式?

Is the only solution to use events or promises? Am I implementing this pattern correctly?

推荐答案

这是一个有点一个超出角度和成JavaScript恕我直言的问题。

this is a bit of a problem that extends beyond angular and into javascript imho.

构建调解,以避免循环依赖的标准方法是使用接口,但JavaScript没有接口。

the standard way to structure mediators to avoid circular dependencies is to use interfaces, but javascript doesn't have interfaces.

在您的情况,如果你有调解注入服务,反之亦然,是会有一个错误。

in your situation, if you have the mediator injected into the service and vice versa, yes there will be an error.

假设你的中介也是一种服务,周围的工作是不是有注入中介的依赖,而是之前都曾经与你需要它来调解为2服务所使用的初始化调解员。

assuming your mediator is also a service, a work around is not to have dependencies injected into the mediator, but rather have the mediator initialized before it is ever used with the 2 services that you need it to mediate for.

另一种方法是不使用调解对象,但使用的是内置角速度与$广播和$的发布/订阅模式。恕我直言,这给比调解模式更加松散的耦合,因此它有我竖起大拇指

the alternative is not to use a mediator object, but use the publisher/subscriber pattern which is built into angular with $broadcast and $on. imho, this gives even looser coupling than the mediator pattern, so it has my thumbs up

这篇关于避免与AngularJS Mediator模式循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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