Angularjs pubsub 与 $broadcast [英] Angularjs pubsub vs $broadcast

查看:33
本文介绍了Angularjs pubsub 与 $broadcast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读 Angularjs 中的事件传递,但我不相信使用 $broadcast 是个好主意.

I've been reading up on event passing in Angularjs and I'm not convinced that using $broadcast is a good idea.

像这样的博客 一个 提倡习惯 $on,即使它感觉有点矫枉过正."

Blogs like this one advocate getting used to $on even though it "felt like overkill."

我的困惑是该实现使用范围的深度优先遍历并查找订阅者,这使得事件的速度取决于您的树结构.这是一些角度的代码:

My confusion is that the implementation uses a depth-first traversal of the scopes and looks for subscribers, which makes the speed of your events dependent on your tree structure. Here is some code from that in angular:

// Insanity Warning: scope depth-first traversal
// yes, this code is a bit crazy, but it works and we have tests to prove it!
// this piece should be kept in sync with the traversal in $digest
if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) {
   while(current !== target && !(next = current.$$nextSibling)) {
     current = current.$parent;
   }
}

此外,您似乎可以使用这些方法破解依赖项注入.

Additionally, it seems like you would be able to hack dependency injection using these methods.

替代方案只是一个缓存事件类型和回调的服务,并直接调用它们.这需要您清理订阅以避免泄漏.

The alternative is simply a service that caches event types and callbacks, and calls them directly. This requires that you clean up the subscriptions to avoid leaks.

我的问题是,我对 $broadcast/$on 范式的动机有什么遗漏吗?或者使用它比使用更传统的发布订阅有什么好处?

My question is, is there anything I'm missing about the motivation for the $broadcast/$on paradigm? Or is there any benefit to use it over a more traditional pubsub?

如果我的问题不够清楚,请告诉我,感谢您抽出宝贵时间.

Let me know if I'm not being clear enough with my question, and thanks for your time.

推荐答案

我认为您没有遗漏任何东西.您已经成功地概述了每种方法的优缺点.

I don't think you are missing anything. You've successfully outlined the pros/cons of each approach.

$broadcast/$on 方法不需要您取消订阅,但它的效率并不高,因为它向所有范围广播.它的进入门槛也非常低.您不需要注入任何服务,也不需要创建它们.他们向所有人广播,所以这是一种更简单的方法.

The $broadcast/$on approach doesn't require you to unsubscribe, but it is not terribly efficient as it broadcasts to all the scopes. It also has a very low barrier to entry. You don't need to inject any services, you don't need to create them. They broadcast to everyone, so it is a more simple approach.

发布/订阅方法更加直接.只有订阅者才能获得事件,因此它不会在系统中的每个范围内使其工作.然而,它更复杂,因为您需要使用回调处理程序编写您的服务,并且您必须记住取消订阅.在我看来,记住取消订阅是非常重要的.如果你没有做到这一点,你会得到内存泄漏.直到 3 个月内出现问题您才会知道.

The pub/sub approach is much more direct. Only subscribers get the events, so it isn't going to every scope in the system to make it work. It is more complex, however, because you need to write your service with callback handlers, and you have to remember to unsubscribe. The remembering to unsubscribe is pretty huge in my opinion. If you don't get this right, you get memory leaks. And you won't know it until it is a problem in 3 months.

我明白为什么内置方法是 $broadcast.

I can see why the built-in approach is $broadcast.

这篇关于Angularjs pubsub 与 $broadcast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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