JSFiddle - React, Tailwind, and code Playground
by vjunloc
HTML
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/themes/ui-lightness/jquery-ui.css">
<div >
<input type = 'text' class = 'validate'/>
<button id = 'btn'>click Me</button>
</div>
JavaScript
// the topic/subscription hash
var pubsub_cache = {};
$.publish = function(/* String */topic, /* Array? */args){
// summary:
// Publish some data on a named topic.
// topic: String
// The channel to publish on
// args: Array?
// The data to publish. Each array item is converted into an ordered
// arguments on the subscribed functions.
//
// example:
// Publish stuff on '/some/topic'. Anything subscribed will be called
// with a function signature like: function(a,b,c){ ... }
//
// | $.publish("/some/topic", ["a","b","c"]);
pubsub_cache[topic] && $.each(pubsub_cache[topic], function(){
this.apply($, args || []);
});
};
$.subscribe = function(/* String */topic, /* Function */callback){
// summary:
// Register a callback on a named topic.
// topic: String
// The channel to subscribe to
// callback: Function
// The handler event. Anytime something is $.publish'ed on a
// subscribed channel, the callback will be called with the
// published array as ordered arguments.
//
// returns: Array
// A handle which can be used to unsubscribe this particular subscription.
//
// example:
// | $.subscribe("/some/topic", function(a, b, c){ /* handle data */ });
//
if(!pubsub_cache[topic]){
pubsub_cache[topic] = [];
}
pubsub_cache[topic].push(callback);
return [topic, callback]; // Array
};
$.unsubscribe = function(/* Array */handle){
// summary:
// Disconnect a subscribed function for a topic.
// handle: Array
// The return value from a $.subscribe call.
// example:
// | var handle =...