pub/sub插件

Intro

pub/sub插件的功能就如同此名字,是设计模式中的发布订阅模式,借助于jQuery的事件系统扩展而成。

Author

name: Ben Alman
concat: https://github.com/cowboy/jquery-tiny-pubsub

Example

http://codepen.io/janking/pen/gMrEPq

Usage

// Creates a "named" logging function.
function createLogger(name) {
  return function(_, a, b) {
    // Skip the first argument (event object) but log the name and other args.
    console.log(name, a, b);
  };
}
// Subscribe to the "foo" topic (bind to the "foo" event, no namespace).
$.sub('foo', createLogger('foo'));
// Subscribe to the "foo.bar" topic (bind to the "foo" event, "bar" namespace).
$.sub('foo.bar', createLogger('foo.bar'));
// Subscribe to the "foo.baz" topic (bind to the "foo" event, "baz" namespace).
$.sub('foo.baz', createLogger('foo.baz'));

// Publish arbitrary values.
$.pub('foo', [1, 2]);
// logs:
// foo 1 2
// foo.bar 1 2
// foo.baz 1 2

results matching ""

    No results matching ""