订阅样式html,发布-订阅模式.html

Title

var pubsub = {};

(function(myObject) {

// Storage for topics that can be broadcast 可以广播的主题的存储

// or listened to 或者听

var topics = {};

// A topic identifier 主题标识符

var subUid = -1;

// Publish or broadcast events of interest 发布或广播感兴趣的事件

// with a specific topic name and arguments 具有特定的主题名称和参数

// such as the data to pass along 如数据传递

myObject.publish = function( topic, args ) {

if ( !topics[topic] ) {

return false;

}

var subscribers = topics[topic],

len = subscribers ? subscribers.length : 0;

while (len--) {

subscribers[len].func( topic, args );

}

return this;

};

// Subscribe to events of interest 订阅感兴趣的事件

// with a specific topic name and a 具有特定的主题名称和

// callback function, to be executed 回调函数,将被执行

// when the topic/event is observed 当观察到主题/事件时

myObject.subscribe = function( topic, func ) {

if (!topics[topic]) {

topics[topic] = [];

}

var token = ( ++subUid ).toString();

topics[topic].push({

token: token,

func: func

});

return token;

};

// Unsubscribe from a specific 取消订阅

// topic, based on a tokenized reference 主题,基于标记化参考

// to the subscription 订阅

myObject.unsubscribe = function( token ) {

for ( var m in topics ) {

if ( topics[m] ) {

for ( var i = 0, j = topics[m].length; i < j; i++ ) {

if ( topics[m][i].token === token ) {

topics[m].splice( i, 1 );

return token;

}

}

}

}

return this;

};

}( pubsub ));

// A simple message logger that logs any topics and data received through our 一个简单的消息记录器,记录通过我们接收的任何主题和数据。

// subscriber 用户

var messageLogger = function ( topics, data ) {

console.log( "Logging: " + topics + ": " + data );

};

// Subscribers listen for topics they have subscribed to and 订阅者监听他们订阅的主题和

// invoke a callback function (e.g messageLogger) once a new 调用一个新的回调函数(例如MasigelgGER)

// notification is broadcast on that topic 这个主题广播通知

var subscription = pubsub.subscribe( "inbox/newMessage", messageLogger );

// Publishers are in charge of publishing topics or notifications of 出版商负责出版主题或通知。

// interest to the application. e.g: 对应用程序有兴趣。例如:

pubsub.publish( "inbox/newMessage", "hello world!" );

// or

pubsub.publish( "inbox/newMessage", ["test", "a", "b", "c"] );

// or

pubsub.publish( "inbox/newMessage", {

sender: "hello@google.com",

body: "Hey again!"

});

// We can also unsubscribe if we no longer wish for our subscribers 如果我们不再希望我们的订户,我们也可以退订。

// to be notified 被通知

pubsub.unsubscribe( subscription );

// Once unsubscribed, this for example won't result in our 一旦退订,这将不会导致我们的

// messageLogger being executed as the subscriber is MasaGelgGER作为订阅服务器执行

// no longer listening 不再听

pubsub.publish( "inbox/newMessage", "Hello! are you still there?" );

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值