From: Mike Taylor Date: Thu, 27 Feb 2014 15:23:56 +0000 (+0000) Subject: Add simple PubSub code adapted from jQuery manual. X-Git-Tag: 1.0.0~1390^2~1 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=5a8ded8d2a75ea7b0ca1412ec837d8f33114edbc;p=mkws-moved-to-github.git Add simple PubSub code adapted from jQuery manual. --- diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index abf8c0a..72ca3f3 100644 --- a/tools/htdocs/mkws.js +++ b/tools/htdocs/mkws.js @@ -142,6 +142,29 @@ var mkws = { }; +// The following PubSub code is modified from the jQuery manual: +// https://api.jquery.com/jQuery.Callbacks/ +// +// Use as: +// mkws.queue("eventName").subscribe(function(param1, param2 ...) { ... }); +// mkws.queue("eventName").publish(arg1, arg2, ...); + +(function() { + var queues = {}; + mkws.queue = function(id) { + if (!queues[id]) { + var callbacks = $.Callbacks(); + queues[id] = { + publish: callbacks.fire, + subscribe: callbacks.add, + unsubscribe: callbacks.remove + }; + } + return queues[id]; + } +}()); + + // Define empty mkws_config for simple applications that don't define it. if (mkws_config == null || typeof mkws_config != 'object') { var mkws_config = {};