You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var pubsub = {};
(function(q) {
var topics = {},
subUid = -1;
// Publish or broadcast events of interest
// with a specific topic name and arguments
// such as the data to pass along
q.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
q.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
q.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 ));
使用1
var messageLogger = function( topic, data ){
console.log(topic,data)
}
pubsub.subscribe('newMessage',messageLogger);
pubsub.publish('newMessage','hello world');
pubsub.publish('newMessage',['red','yellow','blue']);
pubsub.publish('newMessage','hello');
使用2
var getCurrentTime = function (){
var date = new Date(),
m = date.getMonth() + 1,
d = date.getDate(),
y = date.getFullYear(),
t = date.toLocaleTimeString().toLowerCase();
return (m + "/" + d + "/" + y + " " + t);
};
function addGridRow( data ) {
console.log( "updated grid component with:" , data );
}
function updateCounter( data ) {
console.log( "data last updated at: " + getCurrentTime() + " with " , data);
}
var gridUpdate = function( topic, data ){
if ( data !== "undefined" ) {
addGridRow( data );
updateCounter( data );
}
};
var subscriber = pubsub.subscribe( "newDataAvailable", gridUpdate );
pubsub.publish( "newDataAvailable", {
summary: "Apple made $5 billion",
identifier: "APPL",
stockPrice: 570.91
});
pubsub.publish( "newDataAvailable", {
summary: "Microsoft made $20 million",
identifier: "MSFT",
stockPrice: 30.85
});
实现
使用1
使用2
jquery-tiny-pubsub
The text was updated successfully, but these errors were encountered: