You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/06/30 02:38:32 UTC

svn commit: r1141350 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/resources/org/apache/tapestry5/t5-dom.js main/resources/org/apache/tapestry5/t5-pubsub.js test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml

Author: hlship
Date: Thu Jun 30 00:38:31 2011
New Revision: 1141350

URL: http://svn.apache.org/viewvc?rev=1141350&view=rev
Log:
TAP5-999: Allow publishing and subscribing by DOM element id, as well as by element reference

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-dom.js
    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-dom.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-dom.js?rev=1141350&r1=1141349&r2=1141350&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-dom.js (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-dom.js Thu Jun 30 00:38:31 2011
@@ -16,6 +16,22 @@
 T5.define("dom", function() {
 
 	/**
+	 * Locates an element. If element is a string, then
+	 * document.getElementById() is used to resolve a client element id to a DOM
+	 * element. If the id does not exist, then null will be returned.
+	 * <p>
+	 * If element is not a string, it is presumed to already by a DOM element,
+	 * and is returned.
+	 */
+	function locate(element) {
+		if (typeof element == "string") {
+			return document.getElementById(element);
+		}
+
+		return element; // may be null, otherwise presumed to be a DOM node
+	}
+
+	/**
 	 * Tree-walks the children of the element; for each dhild, ensure that all
 	 * event handlers, listeners and PubSub publishers for the child are
 	 * removed.
@@ -53,6 +69,8 @@ T5.define("dom", function() {
 		}
 
 		// Get rid of any Prototype event handlers as well.
+		// May generalize this to be a published message instead, for 
+		// cross-library compatibility.
 		Event.stopObserving(element);
 
 		purgeChildren(element);
@@ -81,6 +99,14 @@ T5.define("dom", function() {
 
 	return {
 		remove : remove,
-		purgeChildren : purgeChildren
+		purgeChildren : purgeChildren,
+		locate : locate
 	};
+});
+
+/**
+ * Create a T5.$() synonym for T5.dom.locate().
+ */
+T5.extend(T5, {
+	$ : T5.dom.locate
 });
\ No newline at end of file

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=1141350&r1=1141349&r2=1141350&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 Jun 30 00:38:31 2011
@@ -16,6 +16,11 @@ T5.define("pubsub", function() {
 	// Element keys: topic, element, publisherfn
 	var publishers = [];
 
+	// Necessary since T5.dom depends on T5.pubsub
+	function $(element) {
+		return T5.$(element);
+	}
+
 	function purgePublisherCache(topic) {
 		each(function(publisher) {
 			if (publisher.topic === topic) {
@@ -53,7 +58,8 @@ T5.define("pubsub", function() {
 	 *            a topic name, which must not be blank
 	 * @param element
 	 *            a DOM element, which may be null to subscribe to all messages
-	 *            for the topic
+	 *            for the topic. If a string, then T5.$() is used to locate the
+	 *            DOM element with the matching client id.
 	 * @param listenerfn
 	 *            function invoked when a message for the topic is published.
 	 *            The function is invoked only if the supplied selector element
@@ -66,7 +72,7 @@ T5.define("pubsub", function() {
 
 		var subscriber = {
 			topic : topic,
-			element : element,
+			element : $(element),
 			listenerfn : listenerfn
 		};
 
@@ -111,11 +117,18 @@ T5.define("pubsub", function() {
 	 *            used to select listeners
 	 * @param element
 	 *            the DOM element used as the source of the published message
-	 *            (also used to select listeners)
+	 *            (also used to select listeners). Passed through T5.$(), the
+	 *            result must not be null.
 	 * @return publisher function used to publish a message
 	 */
 	function createPublisher(topic, element) {
 
+		element = $(element);
+
+		if (element == null) {
+			throw "Element may not be null when creating a publisher.";
+		}
+
 		var existing = first(function(publisher) {
 			return publisher.topic === topic && publisher.element === element;
 		}, publishers);

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=1141350&r1=1141349&r2=1141350&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 Jun 30 00:38:31 2011
@@ -285,7 +285,8 @@
         document.body.insert({bottom: element})
 
         sub("bar", null, function() { pubs.push("general") })
-        sub("bar", element, function() { pubs.push("specific") } )
+        sub("bar", element,
+        function() { pubs.push("specific") } )
 
         pub("bar", element, {})
 
@@ -301,12 +302,36 @@
 
         pubs = []
 
-        pub("bar", document.body, {})
+        pub("bar",
+        document.body, {})
 
         assertEqual(pubs, ["general"])
       </pre>
     </div>
 
+    <div>
+      <p>Publish/subscribe via client id rather than element.</p>
+
+      <pre>
+        var element = new Element("span",{ id: "zaphod" }).update("Published Message Source Zaphod")
+
+        document.body.insert({bottom:
+        element})
+
+        var pubs = []
+
+        sub("bar", "zaphod",
+        function() {
+        pubs.push("specific") } )
+
+        pub("bar",
+        "zaphod", {})
+
+        assertEqual(pubs, ["specific"])
+
+        T5.dom.remove(element)  
+    </pre>
+    </div>
 
   </div>