tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jkuhn...@apache.org
Subject svn commit: r418657 - in /tapestry/tapestry4/trunk/framework/src: java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java js/tapestry/core.js test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java
Date Sun, 02 Jul 2006 22:08:25 GMT
Author: jkuhnert
Date: Sun Jul  2 15:08:25 2006
New Revision: 418657

URL: http://svn.apache.org/viewvc?rev=418657&view=rev
Log:
Added in proper handling of various types of script responses during ajax renders(body/initialization/URL
includes)

Modified:
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java
    tapestry/tapestry4/trunk/framework/src/js/tapestry/core.js
    tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java

Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java?rev=418657&r1=418656&r2=418657&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java
(original)
+++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java
Sun Jul  2 15:08:25 2006
@@ -237,10 +237,10 @@
     {
         IMarkupWriter writer = getWriter(ResponseBuilder.INCLUDE_SCRIPT, ResponseBuilder.SCRIPT_TYPE);
         
-        writer.begin("script");
-        writer.attribute("type", "text/javascript");
-        writer.attribute("src", url);
-        writer.end();
+        writer.printRaw("tapestry.loadScriptFromUrl(\"");
+        writer.print(url);
+        writer.printRaw("\");");
+        
         writer.println();
     }
     

Modified: tapestry/tapestry4/trunk/framework/src/js/tapestry/core.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/js/tapestry/core.js?rev=418657&r1=418656&r2=418657&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/js/tapestry/core.js (original)
+++ tapestry/tapestry4/trunk/framework/src/js/tapestry/core.js Sun Jul  2 15:08:25 2006
@@ -60,7 +60,8 @@
 		}
 		
 		var elms=resp[0].childNodes;
-		var scripts=[];
+		var bodyScripts=[];
+		var initScripts=[];
 		for (var i=0; i<elms.length; i++) {
 			var type=elms[i].getAttribute("type");
 			var id=elms[i].getAttribute("id");
@@ -71,13 +72,24 @@
 				return;
 			}
 			
+			// handle javascript evaluations
 			if (type == "script") {
-				scripts.push(elms[i]);
-				continue;
+				
+				if (id == "initializationscript") {
+					initScripts.push(elms[i]);
+					continue;
+				} else if (id == "bodyScripts") {
+					bodyScripts.push(elms[i]);
+					continue;
+				} else if (id == "includescript") {
+					// includes get processed immediately (syncrhonously)
+					tapestry.loadScriptContent(elms[i], false);
+				}
+				
 			}
 			
 			if (!id) {
-				dojo.raise("No element id found in ajax-response node.");
+			dojo.raise("No element id found in ajax-response node.");
 				return;
 			}
 			
@@ -90,8 +102,12 @@
 			tapestry.loadContent(id, node, elms[i]);
 		}
 		
-		for (var i=0; i<scripts.length; i++) {
-			tapestry.loadScriptContent(scripts[i], true);
+		// load body scripts before initialization
+		for (var i=0; i<bodyScripts.length; i++) {
+			tapestry.loadScriptContent(bodyScripts[i], true);
+		}
+		for (var i=0; i<initScripts.length; i++) {
+			tapestry.loadScriptContent(initScripts[i], true);
 		}
 	},
 	
@@ -130,38 +146,44 @@
 		
         match = new RegExp(tapestry.ScriptFragment, 'im');
         if (async) {
-        	setTimeout(function() {
-        		tapestry.scriptInFlight = true;
-        		
-                for (var i=0; i<scripts.length; i++) {
-                    var scr = scripts[i].match(match)[1];
-                    try {
-                        dojo.log.debug("evaluating script:" + scr);
-                        eval(scr);
-                    } catch (e) {
-                    	tapestry.scriptInFlight = false;
-                        dojo.log.exception("Error evaluating script: " + scr, e, false);
-                    }
-                }
-                
-                tapestry.scriptInFlight = false;
-            }, 60);
+        	setTimeout(function() { 
+        		tapestry.evaluateScripts(scripts, match); 
+        	}, 60);
         } else {
-        	tapestry.scriptInFlight = true;
-        	
-            for (var i=0; i<scripts.length; i++) {
-                var scr = scripts[i].match(match)[1];
-                try {
-                    dojo.log.debug("synchronous eval of script:" + scr);
-                    eval(scr);
-                } catch (e) {
-                	tapestry.scriptInFlight = false;
-                    dojo.log.exception("Error synchronously evaluating script: " + scr, e,
false);
-                }
+        	tapestry.evaluateScripts(scripts, match);
+        }
+	},
+	
+	evaluateScripts:function(scripts, match){
+		tapestry.scriptInFlight = true;
+       	
+        for (var i=0; i<scripts.length; i++) {
+            var scr = scripts[i].match(match)[1];
+            try {
+                dojo.log.debug("evaluating script:" + scr);
+                eval(scr);
+            } catch (e) {
+            	tapestry.scriptInFlight = false;
+                dojo.log.exception("Error evaluating script: " + scr, e, false);
             }
-            
-            tapestry.scriptInFlight = false;
         }
+        
+        tapestry.scriptInFlight = false;
+	},
+	
+	loadScriptFromUrl:function(url){
+	    var scripts = window.document.getElementsByTagName("script");
+	    for (var i = 0; i < scripts.length; i++) {
+	        var src = scripts[i].src;
+	        if (src && src.length > 0 && src.indexOf(url)>=0 ) {
+	            return;
+	        }
+	    }
+		
+	    var e = document.createElement("script");
+	    e.src = url;
+	    e.type = "text/javascript";
+	    document.getElementsByTagName("head")[0].appendChild(e);
 	},
 	
 	presentException:function(node, kwArgs) {

Modified: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java?rev=418657&r1=418656&r2=418657&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java
(original)
+++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java
Sun Jul  2 15:08:25 2006
@@ -245,7 +245,7 @@
         DojoAjaxResponseBuilder builder = new DojoAjaxResponseBuilder(mw, null);
         
         String script1 = "http://noname/js/package.js";
-        String script2 = "http://noname/js/package.js";
+        String script2 = "http://noname/js/package2.js";
         
         verify();
         replay();
@@ -261,9 +261,9 @@
         assertOutput("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC
\"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"
[\n" + 
                 "<!ENTITY nbsp \'&#160;\'>\n" + 
                 "]>\n" + 
-                "<ajax-response><response id=\"includescript\" type=\"script\"><script
type=\"text/javascript\" src=\"http://noname/js/package.js\"></script>" + 
+                "<ajax-response><response id=\"includescript\" type=\"script\">tapestry.loadScriptFromUrl(\"http://noname/js/package.js\");"
+ 
                 LINE_SEPERATOR +
-                "<script type=\"text/javascript\" src=\"http://noname/js/package.js\"></script>"
+
+                "tapestry.loadScriptFromUrl(\"http://noname/js/package2.js\");" +
                 LINE_SEPERATOR +
                 "</response></ajax-response>");
         



Mime
View raw message