简体   繁体   English

如何处理这种循环依赖性?

[英]How to handle this cycle dependency?

I have an design like this: 我有这样的设计:

  1. UI Layer UI层
  2. Component Layer. 组件层。

The idea is all event handlers like click, painting the dom will be hitting UI layer, which will call the corresponding component layer. 想法是所有事件处理程序(例如click),绘制dom都将到达UI层,UI层将调用相应的组件层。

Now imagine on click of a button, the ui layer calls component layer, so the dependencies looks like this: 现在想象一下,单击一个按钮,ui层将调用组件层,因此依赖项如下所示:

define(['creationComponent']) //ui layer module

so that on click of button, this module can call creationComponent methods. 因此,在单击按钮时,此模块可以调用creationComponent方法。

Now creationComponent too, in order to paint something, calls the ui layer, so the creationComponent looks like this: 现在, creationComponent也为了绘画而调用了ui层,因此creationComponent看起来像这样:

define(['uiLayer']) //create module

and here I go with dependencies cycle, where I'm making mistake in the design process? 在这里我经历了依赖周期,在设计过程中犯了错误?

I do not think UI layer should really call component layer if some event happened. 如果发生某些事件,我认为UI层不应该真正调用组件层。 It should send an event. 它应该发送一个事件。 You could use Observer pattern here. 您可以在此处使用观察者模式。 After it sent an event, it should not care if it was handled or not. 发送事件后,它不必关心它是否被处理。

When component needs to draw itself, I guess here an actual call should happen since this action can not be forgotten and its handling cannot be skipped. 当组件需要绘制自身时,我猜这里应该发生实际调用,因为不能忘记此操作并且不能跳过其处理。

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

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