Angularjs发布订阅VS $广播 [英] Angularjs pubsub vs $broadcast

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

问题描述

我一直对事件传递Angularjs读了,我不相信使用$广播是一个好主意。

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

博客像这样的one倡导习惯为$即使它感觉就像矫枉过正。

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

我的困惑是,实现使用范围的深度优先遍历和寻找用户,这使得依赖于你的树结构中的事件的速度。
下面是一些code在角:

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.

我的问题是,有什么我缺少有关的动机为$广播/ $的范式?或者是有任何使用它在一个更传统的pubsub好处?

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.

$广播 / $关于办法并不要求你退订,但它是不是非常有效因为它广播到所有范围。它也有一个非常低的进入壁垒。你并不需要注入任何服务,您不需要创建它们。他们播报给大家,所以它是一个更简单的办法。

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.

发布/订阅方式是更为直接。只有用户可以得到的事件,所以它是不会每个范围的系统,使其工作。这是比较复杂的,但是,因为你需要有回调处理程序写你的服务,你必须记住要退订。退订记忆是pretty在我看来,巨大的。如果你没有得到这个权利,你会得到内存泄漏。而你不会知道它,直到它是在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.

我可以看到为什么内置的方法是 $广播

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

这篇关于Angularjs发布订阅VS $广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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