简体   繁体   English

jQuery Callback和Pub / Sub

[英]jQuery Callback and Pub / Sub

In the past I have done very simple pub / sub in jQuery by binding on the window. 在过去,我通过在窗口上绑定在jQuery中完成了非常简单的pub / sub。

// subscribe
$( window ).on("someEvent", function() { ... });

// publish
$( window ).trigger("someEvent");

However I recently learned about the new callbacks feature, which seems to be the recommended way of handling pub / sub in jQuery. 但是我最近了解了新的回调功能,这似乎是在jQuery中处理pub / sub的推荐方法。

What are the advantages of using callback, as opposed to just binding on the window? 使用回调有什么好处,而不仅仅是在窗口上绑定? For a simple system like the one above, is using the Callback feature overkill? 对于像上面这样的简单系统,使用回调功能是否过度杀伤?

Edit: Here is a bit more info about how I commonly use the above... 编辑:这里有更多关于我如何使用上述内容的信息......

This is something that I will sometimes do to allow my jQuery plugins to talk to each other. 这有点我有时会允许我的jQuery插件相互通信。 For example, I have my own draggable and droppable plugins that need to communicate. 例如,我有自己的需要通信的可拖动和可放置的插件。

When dragging starts, updates and stops, the draggable plugin triggers custom events on the window. 当拖动开始,更新和停止时,可拖动插件会触发窗口上的自定义事件。 The droppable plugin watches these events and reacts accordingly. droppable插件会监视这些事件并做出相应的反应。

// in draggable

onStart: function() {
  $( window ).trigger("dragger.start", [data]);
}

// in droppable

$( window ).on("dragger.start", function(event, data) {
...
});

Binding to the window is not problematic in and of itself, but because other events can be bound to and unbound from the window your system can have side effects that you do not intend. 绑定到窗口本身并不成问题,但由于其他事件可以绑定到窗口并从窗口取消绑定,因此系统可能会产生您不想要的副作用。 For example, $(window).off() will unbind "someEvent" even if it was only your intention to unbind more common events like "scroll" or "click" . 例如, $(window).off()将取消绑定"someEvent"即使只是你打算取消绑定更多常见事件,如"scroll""click"

I wouldn't say that using Callbacks is overkill because it's relatively simple -- I would say even simpler than what you've done: 我不会说使用Callbacks是过度的,因为它相对简单 - 我会说比你做的更简单:

var callbacks = $.Callbacks();
callbacks.add(function () { ... });
callbacks.fire();

That is all you would need to replace your sample code. 这就是替换示例代码所需的全部内容。 One advantage I see immediately is that you don't need to know the name of the event you need to trigger during the trigger phase; 我立即看到的一个优点是您不需要知道在触发阶段需要触发的事件的名称; it's handled more transparently, which is often nice. 它处理得更透明,这通常很好。

You can also add multiple function calls to a single callback or have multiple callbacks simultaneously. 您还可以向单个回调添加多个函数调用,或同时进行多个回调。 This is harder to do if you're just using window . 如果您只是使用window这很难做到。

I think that using jQuery's callbacks ($.Callbacks()) makes for much cleaner code and is much more organized than your method of binding a function to a window. 我认为使用jQuery的回调($ .Callbacks())可以使代码更清晰,并且比将函数绑定到窗口的方法更有条理。 You have more flexibility because you don't have to know the name(s) of the event(s) to be used when firing the callback. 您具有更大的灵活性,因为您不必知道在触发回调时要使用的事件的名称。

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

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