优点/使用事件巨资保持在控制器在复杂的应用程序角同步缺点 [英] Pros/Cons of using events heavily to keep controllers in sync across complex Angular Apps

查看:102
本文介绍了优点/使用事件巨资保持在控制器在复杂的应用程序角同步缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我用大量的事件保持一个角度应用程序的不同部分同步彼此的优点/缺点一些建议吗?

Can anyone give me some advice on the pros / cons of using events heavily to keep different parts of an Angular app in sync with each other?

我使用的是一系列的事件和事件监听器的接线连同多个并发和嵌套意见SPA,通过折角的在 UI路由器。 SPA的布局可能是类似于Gmail时,那里有逻辑屏幕的不同区域(以邮件文件夹列表,顶部导航栏中,左侧区域,主要细节查看等)。

I am considering using a series of events and event listeners to wire together an SPA with multiple concurrent and nested views, provided by Angular's ui-router. The layout of the SPA could be analogous to gmail, where there are logically separate regions of the screen (left area with list of mail folders, top navbar, main details view, etc).

我一直在寻找方法来保持同步所有控制器的问候,他们都需要在同一时间访问模式。当一个模型被顶视图进行更新,如果某些模型的显示在主视图中,我想主视图,以通知的变化,并相应地更新它的图。

I have been looking at ways to keep all the controllers in sync with regards to models they all need to access at the same time. When a model is updated by the top view, if some of that model is displayed in the main view, I want the main view to notice the change, and update its view accordingly.

我用服务共享跨控制器模型,被认为是最好的做法,但也有在深度<一个讨论的问题的方法href=\"http://stackoverflow.com/questions/21414675/angular-ui-router-nested-views-dont-refresh-data-retrieved-by-angular-services\">in这个问题。

I use services to share models across controllers, as is considered the best practice, but there are issues with that approach as discussed in depth in this question.

一个可能的解决方案是使用角的 $广播 $上('事件,FN())有控制器注意到,当他们所关心的已更新数据的服务。

A possible solution to this is to use Angular's $broadcast and .$on('event, fn()) to have controllers notice when a service they care about has updated data.

问题:


  1. 使用事件来传递数据,而不是使用服务(紧耦合,忽略任何权威数据源)。我知道你的可以的传递对象,事件,但你不必。

  2. 性能($整个范围层次等事件气泡)

  1. Using events to pass data instead of using services (tight coupling, ignoring any authoritative data source). I know you can pass object with events, but you don't have to.
  2. Performance (events bubble throughout $scope hierarchy, etc).

$广播 / $发出具备性能和紧密耦合的关注,在这里和其他地方覆盖的,很多和我使用从SO回答<解决方案考虑href=\"http://stackoverflow.com/questions/11252780/whats-the-correct-way-to-communicate-between-controllers-in-angularjs\">here从当控制器被销毁未清理的听众解决性能/内存泄漏。

$broadcast/$emit have performance and tight-coupling concerns that are covered a-plenty here and elsewhere, and I am considering using solution from an SO answer here to address performance / memory leaks from not cleaning up listeners when controllers are destroyed.

维护(意大利面条code /忽略OO和刚好路过你的整个应用程序的模型,事件左右)。
这是最主要的问题,我已经为社会:基于(丑)图下面,我想每个角。服务射击时它就会被更新的模式数据的事件,谁关心这些数据的任何控制器,只听的事件火灾:

Maintenance (spaghetti code / ignoring OO and just passing your entire app's model around in events). This is the main question I have for the community: based on the (ugly) diagram below, I imagine each angular .service firing an event when it gets updated model data, and any controllers who care about that data just listen for the event to fire:

这其实简单,code(现在的工作),但我担心试图让所有事件的关系轨道5年 - 从 - 现在头疼的问题。

It's actually simple to code (works now), but I worry about the 5-years-from-now headache of trying to keep track of all the event relationships.

没有人有任何经验,这样的设计,谁可以给我一个更聪明/更简洁的设计一些建议吗?

Does anyone have any experience with a design like this, who can give me some advice on a smarter / more concise design?

在此先感谢,并为原油图表对不起...希望它得到跨越点。

Thanks in advance, and sorry for the crude diagram... hopefully its gets across the point.

推荐答案

赞成?


  • 这使得角度更像是淘汰赛/迪朗达尔?

缺点?


  • 这使得角度更像是淘汰赛/迪朗达尔?

我已经回答了你链接的问题之一:这里这样可以帮助你。

I've answered one of the questions you linked: here So that might help you.

这也可以回答您的评论的问题。

Which might also answer the question in your comment.

另外我的建议是不会放弃OO。但是,拥抱它,将角然后通过工作的所有的时间报答你。

Also my advice is to not abandon OO. But embrace it, angular will then reward you by working all of the time.

假如你在previosly链接的问题有一个用户对象与密码字段,你可能就不会有问题,因为该密码将一直都是参考 - 能够通过用户。和所有的绑定 user.password的的会工作。

Had you in the previosly linked question Had a User object with a password field you would have never had the issue Because the password would always have been reference-able through the user. And all of the bindings to User.password would have worked.

所以永远只能有一个数据源。保持该引用。和你的双向绑定将修改数据。你不应该担心在事件传播它在你的应用程序。

So only ever have one source of data. Keep that reference. And your two way binding will modify that data. You shouldn't have to worry about propagating it throughout your app in events.

实际上,我工作了各种各样的库模型驱动设计,以帮助(丰富的相关数据)
如果你想贡献我specc-ING出来这里

I'm actually working on a library of sorts to aid in Model Driven Design (rich related data) If you want to contribute I'm specc-ing it out here

这篇关于优点/使用事件巨资保持在控制器在复杂的应用程序角同步缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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