Skip to content

Instantly share code, notes, and snippets.

@lizzie
Created February 20, 2013 05:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizzie/4993046 to your computer and use it in GitHub Desktop.
Save lizzie/4993046 to your computer and use it in GitHub Desktop.
Pub/Sub
var PubSub = {
subscribe: function(ev, callback) {
var calls = this._callbacks || (this._callbacks = {});
(this._callbacks[ev] || (this._callbacks[ev] = [])).push(callback);
return this;
},
publish: function() {
var args = Array.prototype.slice.call(arguments, 0);
var ev = args.shift();
var list, calls, i, l;
if (!(calls = this._callbacks)) return this;
if (!(list = this._callbacks[ev])) return this;
for (i = 0, l = list.length; i < l; i++)
list[i].apply(this, args);
return this;
}
};
PubSub.subscribe("wem", function() {
alert("wem");
});
PubSub.publish("wem");
// from JavaScript Web Applications by MacCaw
// jQuery 版 https://gist.github.com/isao/799721
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment