Updated Branches: refs/heads/master 8593ce85a -> 479fd620e
WICKET-5381 Add Wicket.Event.unsubscribe method Additionally update Grunt configs (copy from master branch) to be able to run the tests Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/fbdd016c Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/fbdd016c Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/fbdd016c Branch: refs/heads/master Commit: fbdd016c1cd15297a54b7240f6bcfea98c2acf1e Parents: 8593ce8 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Mon Sep 30 14:59:30 2013 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Mon Sep 30 15:03:33 2013 +0200 ---------------------------------------------------------------------- .../wicket/ajax/res/js/wicket-event-jquery.js | 19 ++++++ wicket-core/src/test/js/ajax.js | 14 ++-- wicket-core/src/test/js/event.js | 67 ++++++++++++++++++++ 3 files changed, 93 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/fbdd016c/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js index 12cb75e..bfe9c28 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js @@ -177,6 +177,25 @@ }, /** + * Un-subscribes a subscriber from a topic. + * @param topic {String} - the topic name. If omitted un-subscribes all + * subscribers from all topics + * @param subscriber {Function} - the handler to un-subscribe. If omitted then + * all subscribers are removed from this topic + */ + unsubscribe: function(topic, subscriber) { + if (topic) { + if (subscriber) { + jQuery(document).off(topic, subscriber); + } else { + jQuery(document).off(topic); + } + } else { + jQuery(document).off(); + } + }, + + /** * Sends a notification to all subscribers for the given topic. * Subscribers for topic '*' receive the actual topic as first parameter, * otherwise the topic is not passed to subscribers which listen for specific http://git-wip-us.apache.org/repos/asf/wicket/blob/fbdd016c/wicket-core/src/test/js/ajax.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/js/ajax.js b/wicket-core/src/test/js/ajax.js index b42c5a8..5d4aafe 100644 --- a/wicket-core/src/test/js/ajax.js +++ b/wicket-core/src/test/js/ajax.js @@ -69,7 +69,7 @@ jQuery(document).ready(function() { module('Wicket.Ajax', { setup: function() { // unsubscribe all global listeners - jQuery(document).off(); + Wicket.Event.unsubscribe(); } }); @@ -511,7 +511,7 @@ jQuery(document).ready(function() { equal(attrs.u, attributes.u, 'Complete: attrs'); // unregister all subscribers - jQuery(document).off(); + Wicket.Event.unsubscribe(); }); Wicket.Ajax.ajax(attrs); @@ -560,7 +560,7 @@ jQuery(document).ready(function() { equal(attrs.u, attributes.u, 'Complete: attrs'); // unregister all subscribers - jQuery(document).off(); + Wicket.Event.unsubscribe(); }); Wicket.Ajax.ajax(attrs); @@ -660,7 +660,7 @@ jQuery(document).ready(function() { ok(settings.url.indexOf('two=2') > 0, 'Parameter "two" with value "2" is found'); start(); - jQuery(document).off(); + Wicket.Event.unsubscribe(); }); Wicket.Ajax.ajax(attrs); @@ -697,7 +697,7 @@ jQuery(document).ready(function() { ok(settings.data.indexOf('one=dynamic2') > -1, 'Parameter "one" with value "dynamic2" is found'); start(); - jQuery(document).off(); + Wicket.Event.unsubscribe(); }); Wicket.Ajax.ajax(attrs); @@ -835,7 +835,7 @@ jQuery(document).ready(function() { if (attrs.event.extraData.round === 2) { // unregister all global subscribers - jQuery(document).off(); + Wicket.Event.unsubscribe(); jQuery(window).off("event1"); start(); @@ -942,7 +942,7 @@ jQuery(document).ready(function() { if (attrs.event.extraData.round === 2) { // unregister all global subscribers - jQuery(document).off(); + Wicket.Event.unsubscribe(); jQuery(window).off("event1"); http://git-wip-us.apache.org/repos/asf/wicket/blob/fbdd016c/wicket-core/src/test/js/event.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/js/event.js b/wicket-core/src/test/js/event.js index cae6d94..b8535c8 100644 --- a/wicket-core/src/test/js/event.js +++ b/wicket-core/src/test/js/event.js @@ -218,6 +218,73 @@ jQuery(document).ready(function() { Wicket.Event.publish('topicName'); }); + test('unsubscribe a signle subscriber', function() { + expect(2); + + var topic = "someTopicName"; + + var subscriber = function() { + ok(true, "The subscriber is notified"); + }; + + Wicket.Event.subscribe(topic, subscriber); + + Wicket.Event.publish(topic); + + Wicket.Event.unsubscribe(topic, subscriber); + ok(true, "The subscriber is un-subscribed") + + Wicket.Event.publish(topic); + }); + + test('unsubscribe all subscribers per topic', function() { + expect(3); + + var topic = "someTopicName"; + + var subscriber1 = function() { + ok(true, "Subscriber 1 is notified"); + }; + + var subscriber2 = function() { + ok(true, "Subscriber 2 is notified"); + }; + + Wicket.Event.subscribe(topic, subscriber1); + Wicket.Event.subscribe(topic, subscriber2); + + Wicket.Event.publish(topic); + + Wicket.Event.unsubscribe(topic); + ok(true, "The subscribers are un-subscribed") + + Wicket.Event.publish(topic); + }); + + test('unsubscribe all subscribers (for all topics)', function() { + expect(3); + + var topic = "someTopicName"; + + var subscriber1 = function() { + ok(true, "Subscriber 1 is notified"); + }; + + var subscriber2 = function() { + ok(true, "Subscriber 2 is notified"); + }; + + Wicket.Event.subscribe(topic, subscriber1); + Wicket.Event.subscribe(topic, subscriber2); + + Wicket.Event.publish(topic); + + Wicket.Event.unsubscribe(); + ok(true, "The subscribers are un-subscribed") + + Wicket.Event.publish(topic); + }); + test('all topics', function() { expect(8);
