Skip to content

Commit

Permalink
Added vanilla and underscore versions
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelander committed Jun 27, 2012
1 parent 32ca561 commit eb99647
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 6 deletions.
12 changes: 6 additions & 6 deletions jquery-subtopic.js
Expand Up @@ -15,7 +15,7 @@
*/
(function($) {
"use strict";
var cache = {};
var cache = {},
/**
* Events.publish
* e.g.: Events.publish("/Article/added", [article], this);
Expand All @@ -26,7 +26,7 @@
* @param args {Array}
* @param scope {Object} Optional
*/
var publish = function(topic, args, scope) {
publish = function(topic, args, scope) {
var topics = topic.split('/');
while (topics[0]) {
if (cache[topic]) {
Expand All @@ -40,7 +40,7 @@
topics.pop();
topic = topics.join('/');
}
};
},
/**
* Events.subscribe
* e.g.: Events.subscribe("/Article/added", Articles.validate)
Expand All @@ -51,13 +51,13 @@
* @param callback {Function}
* @return Event handler {Array}
*/
var subscribe = function(topic, callback) {
subscribe = function(topic, callback) {
if (!cache[topic]) {
cache[topic] = [];
}
cache[topic].push(callback);
return [topic, callback];
};
},
/**
* Events.unsubscribe
* e.g.: var handle = Events.subscribe("/Article/added", Articles.validate);
Expand All @@ -69,7 +69,7 @@
* @param completly {Boolean}
* @return {type description }
*/
var unsubscribe = function(handle, completly) {
unsubscribe = function(handle, completly) {
var t = handle[0],
i = cache[t].length - 1;

Expand Down
94 changes: 94 additions & 0 deletions subtopic.js
@@ -0,0 +1,94 @@
/**
* Pub/Sub for Loosely Coupled logic.
*
* Based on Hiroshi Kuwabara's jQuery version of Peter Higgins' port
* from Dojo to JQuery.
*
* Hiroshi Kuwabara's JQuery version
* https://github.com/kuwabarahiroshi/bloody-jquery-plugins/blob/master/pubsub.js
*
* Peter Higgins' port from Dojo to jQuery
* https://github.com/phiggins42/bloody-jquery-plugins/blob/master/pubsub.js
*
* Perfomance enhanced version based on Luís Couto
* https://github.com/phiggins42/bloody-jquery-plugins/blob/55e41df9bf08f42378bb08b93efcb28555b61aeb/pubsub.js
*/
var subtopic = (function() {
"use strict";
var cache = {},
/**
* Events.publish
* e.g.: Events.publish("/Article/added", [article], this);
*
* @class Events
* @method publish
* @param topic {String}
* @param args {Array}
* @param scope {Object} Optional
*/
publish = function(topic, args, scope) {
var topics = topic.split('/');
while (topics[0]) {
if (cache[topic]) {
var thisTopic = cache[topic],
i = thisTopic.length - 1;

for (i; i >= 0; i -= 1) {
thisTopic[i].apply(scope || this, args || []);
}
}
topics.pop();
topic = topics.join('/');
}
},
/**
* Events.subscribe
* e.g.: Events.subscribe("/Article/added", Articles.validate)
*
* @class Events
* @method subscribe
* @param topic {String}
* @param callback {Function}
* @return Event handler {Array}
*/
subscribe = function(topic, callback) {
if (!cache[topic]) {
cache[topic] = [];
}
cache[topic].push(callback);
return [topic, callback];
},
/**
* Events.unsubscribe
* e.g.: var handle = Events.subscribe("/Article/added", Articles.validate);
* Events.unsubscribe(handle);
*
* @class Events
* @method unsubscribe
* @param handle {Array}
* @param completly {Boolean}
* @return {type description }
*/
unsubscribe = function(handle, completly) {
var t = handle[0],
i = cache[t].length - 1;

if (cache[t]) {
for (i; i >= 0; i -= 1) {
if (cache[t][i] === handle[1]) {
cache[t].splice(cache[t][i], 1);
if (completly) {
delete cache[t];
}
}
}
}
};

return {
publish: publish,
subscribe: subscribe,
unsubscribe: unsubscribe
};

}());
92 changes: 92 additions & 0 deletions underscore-subtopic.js
@@ -0,0 +1,92 @@
/**
* Underscore Pub/Sub plugin for Loosely Coupled logic.
*
* Based on Hiroshi Kuwabara's jQuery version of Peter Higgins' port
* from Dojo to JQuery.
*
* Hiroshi Kuwabara's JQuery version
* https://github.com/kuwabarahiroshi/bloody-jquery-plugins/blob/master/pubsub.js
*
* Peter Higgins' port from Dojo to jQuery
* https://github.com/phiggins42/bloody-jquery-plugins/blob/master/pubsub.js
*
* Perfomance enhanced version based on Luís Couto
* https://github.com/phiggins42/bloody-jquery-plugins/blob/55e41df9bf08f42378bb08b93efcb28555b61aeb/pubsub.js
*/
(function(_) {
"use strict";
var cache = {},
/**
* Events.publish
* e.g.: Events.publish("/Article/added", [article], this);
*
* @class Events
* @method publish
* @param topic {String}
* @param args {Array}
* @param scope {Object} Optional
*/
publish = function(topic, args, scope) {
var topics = topic.split('/');
while (topics[0]) {
if (cache[topic]) {
var thisTopic = cache[topic],
i = thisTopic.length - 1;

for (i; i >= 0; i -= 1) {
thisTopic[i].apply(scope || this, args || []);
}
}
topics.pop();
topic = topics.join('/');
}
},
/**
* Events.subscribe
* e.g.: Events.subscribe("/Article/added", Articles.validate)
*
* @class Events
* @method subscribe
* @param topic {String}
* @param callback {Function}
* @return Event handler {Array}
*/
subscribe = function(topic, callback) {
if (!cache[topic]) {
cache[topic] = [];
}
cache[topic].push(callback);
return [topic, callback];
},
/**
* Events.unsubscribe
* e.g.: var handle = Events.subscribe("/Article/added", Articles.validate);
* Events.unsubscribe(handle);
*
* @class Events
* @method unsubscribe
* @param handle {Array}
* @param completly {Boolean}
* @return {type description }
*/
unsubscribe = function(handle, completly) {
var t = handle[0],
i = cache[t].length - 1;

if (cache[t]) {
for (i; i >= 0; i -= 1) {
if (cache[t][i] === handle[1]) {
cache[t].splice(cache[t][i], 1);
if (completly) {
delete cache[t];
}
}
}
}
};

_.publish = publish;
_.subscribe = subscribe;
_.unsubscribe = unsubscribe;

}(underscore));

0 comments on commit eb99647

Please sign in to comment.