Author: hlship
Date: Thu Jul 21 18:19:30 2011
New Revision: 1149295
URL: http://svn.apache.org/viewvc?rev=1149295&view=rev
Log:
TAP5-999: Pass meta data as second parameter to listener functions, allowing
the originating element to be identified, and the message to be canceled
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js?rev=1149295&r1=1149294&r2=1149295&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
Thu Jul 21 18:19:30 2011
@@ -68,9 +68,9 @@ T5.define("pubsub", function() {
* and returned to the publisher.
*
* The listener function is passed a message object as the
first parameter; this is provided
- * on each call to the topic's publish function. The second
parameter is the element
- * defined by the publisher (this is useful when subscribed to
all elements). For truly global
- * events, the document object is used as the element.
+ * on each call to the topic's publish function. The second
parameter is an object with two
+ * properties: An element property to identify the source of
the message, and a cancel() function property
+ * that prevents further listeners from being invoked.
* @return a function of no arguments used to unsubscribe the listener
*/
function subscribe(topic, element, listenerfn) {
@@ -111,6 +111,12 @@ T5.define("pubsub", function() {
* invoked listener functions.
*
* <p>
+ * Listener functions are passed the message object and a second
(optional) object.
+ * The second object contains two keys: The first, "element", identifies
the element for which the publisher was created, i.e.,
+ * the source of the message. The second, "cancel", is a function used to
prevent further listeners
+ * from being invoked.
+ *
+ * <p>
* There is not currently a way to explicitly remove a publisher; however,
* when the DOM element is removed properly, all publishers and subscribers
* for the specific element will be removed as well.
@@ -154,9 +160,29 @@ T5.define("pubsub", function() {
publisher.element);
}
- return map(function(listenerfn) {
- return listenerfn(message, publisher.element);
- }, publisher.listeners);
+ var canceled = false;
+
+ var meta = {
+ element : publisher.element,
+ cancel : function() {
+ canceled = true;
+ }
+ };
+
+ var result = [];
+
+ for (var i = 0; i < publisher.listeners.length; i++) {
+
+ var listenerfn = publisher.listeners[i];
+
+ result.push(listenerfn(message, meta));
+
+ if (canceled) {
+ break;
+ }
+ }
+
+ return result;
}
};
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml?rev=1149295&r1=1149294&r2=1149295&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml
Thu Jul 21 18:19:30 2011
@@ -237,7 +237,7 @@
<div>
<p> Ensure that the second object to a listener is the source object.</p>
<pre>
- var unsub = sub("foo", null, function(message, element) {
assertSame(element, document); })
+ var unsub = sub("foo", null, function(message, meta) {
assertSame(meta.element, document); })
pub("foo", document, null)
@@ -354,6 +354,22 @@
</pre>
</div>
+<div>
+ <p>Listeners invocations can be cancelled.</p>
+
+ <pre>
+ var pubs = []
+
+ sub("bazz", null, function() { pubs.push("first"); })
+ sub("bazz", null, function(message, meta) { pubs.push("second");
meta.cancel(); })
+ sub("bazz", null, function() { pubs.push("third"); })
+
+ pub("bazz", {})
+
+ assertEqual(pubs, ["first", "second" ]);
+ </pre>
+</div>
+
</div>
<script>