JS 的基于主题的发布/订阅
我希望能够拥有类似于 jQuery 自定义事件和 PubSubJS (http://github.com/mroderick/PubSubJS) 中的 pubsub 机制。
问题是这些 pubsub 库中的每一个都在主题上进行了完全匹配。 IO 希望能够发布这样的主题:
"Order/Sent/1234"
并收听订阅:
"Order/Sent/1234"
"Order/Sent/*"
"Order/*/1234"
有谁知道 JS 有这样的东西吗?
I want to be able to have a pubsub mechanism similar to found in jQuery's custom events and PubSubJS (http://github.com/mroderick/PubSubJS).
The problem is that each one of these pubsub libraries does an exact match on the subject. IO want to be able to publish a subject like:
"Order/Sent/1234"
And have a listen subscribe to either:
"Order/Sent/1234"
"Order/Sent/*"
"Order/*/1234"
Does anyone know of anything like this for JS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修改你喜欢的就可以了。在 github 上分叉它,打开
pubsub.js
并执行以下操作:您可能需要对其进行一些修改,例如将所有
*
替换为 < code>.* 或[^\/]*
在转换为正则表达式之前,等等...Just modify the one you like. Fork it on github, go open
pubsub.js
and do something like:You'll probably need to modify that a bit, do something like replace all
*
's with.*
or[^\/]*
before converting to a regex, etc...您必须稍微修改代码
if (property.match(topic)) {
以满足您的要求。You will have to modify the code
if (property.match(topic)) {
a bit to satisfy your requirement.