发布/订阅JavaScript对象 待我称王封你为后i 2023-02-22 03:46 42阅读 0赞 There are three keys to effective AJAX-driven websites: [event delegation][], [History management][], and effective app-wide communication with pub/sub. This blog employs of all of these techniques, and I thought I'd share the simplest of them: a tiny pub/sub module I use on this site. 有效的AJAX驱动网站有三个关键: [事件委托][event delegation] , [历史记录管理][History management]以及与pub / sub进行有效的应用程序范围内的通信。 这个博客使用了所有这些技术,我想我将分享其中最简单的一种:我在本网站上使用的一个很小的发布/订阅模块。 If you've not used pub/sub before, the gist is that you publish to a topic and anyone can subscribe, much like the way a radio works: a radio station broadcasts (publishes) and anyone can listen (subscribes). This is excellent for highly modular web applications; it's a license to globally communicate without attaching to any specific object. 如果您以前没有使用过pub / sub,则要点是您发布一个主题,任何人都可以订阅,就像广播的工作方式一样:广播电台广播(发布),任何人都可以收听(订阅)。 这对于高度模块化的Web应用程序非常有用; 这是在不附加任何特定对象的情况下进行全局通信的许可证。 ## JavaScript **(**The JavaScript**)** ## The module itself is super tiny but massively useful: 该模块本身很小,但是非常有用: var events = (function(){ var topics = {}; var hOP = topics.hasOwnProperty; return { subscribe: function(topic, listener) { // Create the topic's object if not yet created if(!hOP.call(topics, topic)) topics[topic] = []; // Add the listener to queue var index = topics[topic].push(listener) -1; // Provide handle back for removal of topic return { remove: function() { delete topics[topic][index]; } }; }, publish: function(topic, info) { // If the topic doesn't exist, or there's no listeners in queue, just leave if(!hOP.call(topics, topic)) return; // Cycle through topics queue, fire! topics[topic].forEach(function(item) { item(info != undefined ? info : {}); }); } }; })(); Publishing to a topic: 发布到主题: events.publish('/page/load', { url: '/some/url/path' // any argument }); ...and subscribing to said topic in order to be notified of events: ...并订阅该主题,以便收到事件通知: var subscription = events.subscribe('/page/load', function(obj) { // Do something now that the event has occurred }); // ...sometime later where I no longer want subscription... subscription.remove(); I use pub/sub religiously on this website and this object has done me a world of good. I have one topic that fires upon each AJAX page load, and several subscriptions fire during that event (ad re-rendering, comment re-rendering, social button population, etc.). Evaluate your application and see where you might be able to use pub/sub! 我在该网站上虔诚地使用pub / sub,这个对象为我创造了一个美好的世界。 我有一个主题会在每次AJAX页面加载时触发,并且在该事件期间会触发多个订阅(广告重新渲染,评论重新渲染,社交按钮填充等)。 评估您的应用程序,看看您可以在哪里使用pub / sub! > 翻译自: [https://davidwalsh.name/pubsub-javascript][https_davidwalsh.name_pubsub-javascript] [event delegation]: https://davidwalsh.name/event-delegate [History management]: https://davidwalsh.name/mootools-history [https_davidwalsh.name_pubsub-javascript]: https://davidwalsh.name/pubsub-javascript
相关 发布订阅 *发布订阅** 在软件架构中,发布订阅是一种消息范式,消息的发送者(称为发布者)不会将消息直接发送给特定的接收者(称为订阅者)。而是将发布的消息分为不同的类别,无需了... 矫情吗;*/ 2024年04月17日 15:32/ 0 赞/ 179 阅读
相关 JavaScript 的发布订阅模式 发布-订阅模式里面包含了三个模块,发布者,订阅者和处理中心,如下图结构。 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_s 今天药忘吃喽~/ 2023年02月22日 12:18/ 0 赞/ 98 阅读
相关 发布/订阅JavaScript对象 There are three keys to effective AJAX-driven websites: [event delegation][], [History 待我称王封你为后i/ 2023年02月22日 03:46/ 0 赞/ 43 阅读
相关 SpringBoot中RedisTemplate订阅发布对象 解说 > 1. RedisMessageListenerContainer Redis订阅发布的监听容器,你的消息发布、订阅配置都必须在这里面实现 > \ 缺乏、安全感/ 2023年01月21日 05:20/ 0 赞/ 46 阅读
相关 JavaScript的发布订阅模式 ![d355b67bcf963d7647386b650f14a8ef.png][] 这里要说明一下什么是发布-订阅模式。 发布-订阅模式里面包含了三个模块,发布者 你的名字/ 2022年08月29日 11:57/ 0 赞/ 280 阅读
相关 发布--订阅 定义 发布-订阅模式又叫观察者模式,它定义对象间的一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于他的对象都将得到通知。我们一般用事件模型来代替传统的发布 傷城~/ 2022年06月07日 13:44/ 0 赞/ 372 阅读
相关 JavaScript事件发布/订阅 1、发布/订阅模式也是诸多设计模式当中的一种; 2、这种方式可以在es5下相当优雅地处理异步操作; 3、什么是发布/订阅呢?我们举个栗子: 假设fn1,fn2,fn3都可 谁借莪1个温暖的怀抱¢/ 2022年05月21日 08:28/ 0 赞/ 257 阅读
相关 【JavaScript】设计模式-发布订阅模式 var Observer = (function() { var _message = {} return { //将订阅者注册 曾经终败给现在/ 2022年04月23日 21:48/ 0 赞/ 266 阅读
相关 JavaScript发布订阅者模式 假如你要建立一个网站,通常来说会有许多用户。你作为一名管理者,有时候需要将重要的消息发布给你的用户。在软件开发领域,开发此功能往往用到发布订阅者模式。下面我以简单的javasc 朴灿烈づ我的快乐病毒、/ 2022年01月20日 02:09/ 0 赞/ 368 阅读
还没有评论,来说两句吧...