使用重大活动的优点/缺点是使控制器在复杂的角色应用程序中保持同步 [英] Pros/Cons of using events heavily to keep controllers in sync across complex Angular Apps

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

问题描述

任何人都可以给我一些关于使用事件的利弊的建议,以使Angular应用程序的不同部分彼此同步?



我正在考虑使用一系列事件和事件监听器将SPA与多个并发和嵌套视图相连,由Angular的因此可能有帮助你。



哪些也可以回答你的评论中的问题。



另外我的建议是不要放弃OO。但是拥抱它,有时候会通过全部工作来奖励你。



如果你在先前链接的问题有一个用户对象与一个密码字段你永远不会有问题因为密码一直是通过用户可引用的。并且所有绑定到 User.password 将会工作。



所以只有一个数据源。保留该参考。而您的双向绑定将修改该数据。您不必担心在事件中的整个应用中传播它。


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?

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.

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.

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.

Problems:

  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).

    $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.

  3. 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:

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.

解决方案

Pros?

  • It makes angular more like Knockout/Durandal?

Cons?

  • It makes angular more like Knockout/Durandal?

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

Which might also answer the question in your comment.

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

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.

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

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