Created
February 6, 2019 19:51
-
-
Save Kcko/9f6cc70ca71dbba37294b0eba75aa47b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
var o = $({}); | |
var calledEvents = {}; | |
$.subscribe = function() { | |
o.on.apply(o, arguments); | |
if (arguments[0] in calledEvents) | |
{ | |
var eventName = arguments[0]; | |
var callback = arguments[1]; | |
var clbArgs = [ | |
{type: eventName}, // event | |
].concat(calledEvents[eventName][0]); | |
callback.apply(o, clbArgs); | |
} | |
}; | |
$.unsubscribe = function() { | |
o.off.apply(o, arguments); | |
}; | |
$.publish = function() { | |
o.trigger.apply(o, arguments); | |
var eventName = arguments[0]; | |
var args = Array.prototype.slice.call(arguments, 1); | |
calledEvents[eventName] = args; | |
}; | |
}(jQuery)); | |
// USAGE | |
$.subscribe('document/kunda', function(e, a, b) { | |
console.log('A'); | |
}); | |
$.publish('document/kunda', ['AA', 'BBB']); | |
$.subscribe('document/kunda', function(e, a, b) { | |
console.log('B'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment