- jQuery Design Patterns
- Thodoris Greasidis
- 583字
- 2021-07-16 12:52:28
How it is adopted by jQuery
Once again, the jQuery library provides us with a convenient way to take advantage of the Pub/Sub Pattern in our code. Instead of extending its API by adding new methods specifically named "publish" and "subscribe" and introducing new concepts, the developers decided to extend the jQuery.fn.on()
and jQuery.fn.trigger()
methods with the ability to handle and emit custom events. This way, jQuery can be used to implement a publisher/subscriber communication scheme using the already known convenient methods it provides.
Custom events in jQuery
Custom events allow us to use almost any user-defined string value as a common event that we can add listeners for, and also manually fire it on page elements. As an extra but a precious feature, custom events can also carry some extra data to be delivered to the listeners of the event.
The jQuery library added its own custom events implementation, before it was actually added to any web specification. This way, it was proved how useful they can be when used in web development. As we saw in the previous chapter, in jQuery, there is a specific part of the implementation that handles both the common element event and also custom events. The jQuery.event
object holds all the internal implementations related to firing and listening to events. Also, the jQuery.Event
class is a dedicated wrapper that jQuery uses for the needs of both the common element events and its custom events implementation.
Implementing a Pub/Sub scheme using custom events
In the previous chapter, we saw how the jQuery.fn.on()
method can be used to add event listeners on elements. We also saw that its implementation is maintaining lists with the added handlers and notifying them when required. Moreover, the event name seems to have the same coordination purpose, just like the topic. This implementation semantics seem to match exactly with the Pub/Sub Pattern as well.
The jQuery.fn.trigger()
method actually uses the internal jQuery.event.trigger()
method that is used to fire events in jQuery. It iterates over the internal handlers list and executes them with the requested event along with any extra parameters that the custom event defines. Once again, this also matches the operation requirements of the Pub/Sub Pattern.
As a result, jQuery.fn.trigger()
and jQuery.fn.on()
seem to match the needs of the Pub/Sub Pattern and can be used instead of separate "publish" and "subscribe" methods, respectively. Since they are both available on the jQuery.fn
object, we can use these methods on any jQuery object. This jQuery object will act as an intermediate entity between the publishers and the subscribers, in a way that perfectly aligns with the definition of the broker.
A good common practice, which is also used by a lot of jQuery plugins, is to use the outermost page element that holds the implementation of the application or the plugin as the broker. On the other hand, jQuery actually allows us to use any object as a broker, since all that it actually needs is a target to emit an observe for our custom events. As a result, we could even use an empty object as our broker such as $({})
, in case using a page element seems too restricting or not clean enough according to the Pub/Sub Pattern. This is actually what the jQuery Tiny Pub/Sub library does, along with some method aliasing, so that we actually use methods named "publish" and "subscribe" instead of jQuery's "on" and "trigger". For more information on Tiny, you can visit its repository page at https://github.com/cowboy/jquery-tiny-pubsub.
- R語(yǔ)言數(shù)據(jù)分析從入門到精通
- C語(yǔ)言程序設(shè)計(jì)(第3版)
- Rust實(shí)戰(zhàn)
- PyTorch自動(dòng)駕駛視覺(jué)感知算法實(shí)戰(zhàn)
- C# Programming Cookbook
- Learning Linux Binary Analysis
- 差分進(jìn)化算法及其高維多目標(biāo)優(yōu)化應(yīng)用
- Instant Ext.NET Application Development
- Python程序設(shè)計(jì)教程
- Visual C++從入門到精通(第2版)
- Python應(yīng)用與實(shí)戰(zhàn)
- 數(shù)據(jù)結(jié)構(gòu)與算法詳解
- Learning RSLogix 5000 Programming
- 算法學(xué)習(xí)與應(yīng)用從入門到精通
- Storm Real-Time Processing Cookbook