You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2012/07/07 02:35:20 UTC

[1/2] git commit: Change Ajax partial page updates to use the same core/pageinit module as traditional renders Strip out the code in Tapestry.ScriptManager that's related to scripts, as that's done by RequireJS now Add extra debugging (at least temporari

Updated Branches:
  refs/heads/5.4-js-rewrite 3d84263dd -> cea40f5af


Change Ajax partial page updates to use the same core/pageinit module as traditional renders
Strip out the code in Tapestry.ScriptManager that's related to scripts, as that's done by RequireJS now
Add extra debugging (at least temporarily) for initialization
Temporary hack to coordinate initializations with the Tapestry.onDomLoadedCallback function
Extend core/pageinit:initialize to track when all initializations have completed and invoke an optional callback


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/cea40f5a
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/cea40f5a
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/cea40f5a

Branch: refs/heads/5.4-js-rewrite
Commit: cea40f5af9ffdb51fa04cf92bafc3460f31272fe
Parents: 9e447c8
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Jul 6 17:35:04 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Jul 6 17:35:04 2012 -0700

----------------------------------------------------------------------
 .../tapestry5/corelib/modulejs/pageinit.coffee     |   36 ++++-
 .../resources/org/apache/tapestry5/tapestry.js     |  103 ++-------------
 2 files changed, 42 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/cea40f5a/tapestry-core/src/main/coffeescript/org/apache/tapestry5/corelib/modulejs/pageinit.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/org/apache/tapestry5/corelib/modulejs/pageinit.coffee b/tapestry-core/src/main/coffeescript/org/apache/tapestry5/corelib/modulejs/pageinit.coffee
index bdaed47..41fc9d2 100644
--- a/tapestry-core/src/main/coffeescript/org/apache/tapestry5/corelib/modulejs/pageinit.coffee
+++ b/tapestry-core/src/main/coffeescript/org/apache/tapestry5/corelib/modulejs/pageinit.coffee
@@ -20,19 +20,40 @@
 # e.g., "my/module:myfunc".
 # Any additional values in the initializer are passed to the function. The context of the function (this) is null.
 define ["_", "core/console"], (_, console) ->
-  invokeInitializer = (qualifiedName, initArguments...) ->
+  invokeInitializer = (tracker, qualifiedName, initArguments) ->
     [moduleName, functionName] = qualifiedName.split ':'
 
     require [moduleName], (moduleLib) ->
       fn = if functionName? then moduleLib[functionName] else moduleLib
+      console.debug "Invoking #{qualifiedName} with " + JSON.stringify(initArguments)
       fn.apply null, initArguments
 
+      tracker()
+
   exports =
     # Passed a list of initializers, executes each initializer in order. Due to asynchronous loading
     # of modules, the exact order in which initializer functions are invoked is not predictable.
-    initialize: (inits) ->
-      # apply will split the first value from the rest for us, implicitly.
-      invokeInitializer.apply null, init for init in inits
+    initialize: (inits = [], callback) ->
+
+      callbackCountdown = inits.length + 1
+
+      # tracker gets invoked once after each require/callback, plus once extra
+      # (to handle the case where there are no inits). When the count hits zero,
+      # it invokes the callback (if there is one).
+
+      tracker = ->
+        callbackCountdown--
+
+        if callbackCountdown is 0 and callback
+          console.debug "Invoking post-initialization callback function"
+          callback()
+
+      # First value in each init is the qualified module name; anything after
+      # that are arguments to be passed to the identified function.
+      for [qualifiedName, initArguments...] in inits
+        invokeInitializer tracker, qualifiedName, initArguments
+
+      tracker()
 
     # Pre-loads a number of scripts in order. When the last script is loaded,
     # invokes the callback (with no parameters).
@@ -43,14 +64,17 @@ define ["_", "core/console"], (_, console) ->
 
       finalCallback = _.reduceRight scripts, reducer, callback
 
-      finalCallback.call(null)
+      finalCallback.call null
 
     # Loads all the scripts, in order. It then executes the immediate initializations.
     # After that, it waits for the DOM to be ready and executes the other initializations.
     loadScriptsAndInitialize: (scripts, immediateInits, otherInits) ->
       exports.loadScripts scripts, ->
         exports.initialize immediateInits
-        require ["core/domReady!"], -> exports.initialize otherInits
+        # This is where we want to get:
+        # require ["core/domReady!"], -> exports.initialize otherInits
+        # But for compatibility, this is what we're stuck with:
+        Tapestry.onDOMLoaded -> exports.initialize otherInits
 
     evalJavaScript: (js) ->
       console.debug "Evaluating: #{js}"

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/cea40f5a/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js b/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
index 12f6ee2..8a0a5d1 100644
--- a/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
+++ b/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
@@ -185,8 +185,6 @@ var Tapestry = {
 
         Tapestry.pageLoaded = true;
 
-        Tapestry.ScriptManager.initialize();
-
         $$(".t-invisible").each(function (element) {
             element.hide();
             element.removeClassName("t-invisible");
@@ -312,10 +310,13 @@ var Tapestry = {
      * <dt>redirectURL</dt>
      * <dd>URL to redirect to (in which case, the callback is not invoked)</dd>
      * <dt>inits</dt>
-     * <dd>Defines a set of calls to Tapestry.init() to perform initialization
+     * <dd>Passed to module "core/pageinit:initialize" if non-null
      * after the DOM has been updated.</dd>
      * <dt>stylesheets</dt>
      * <dd>Array of hashes, each hash has key href and optional key media</dd>
+     * <dt>scripts</dt>
+     * <dd>URLs of static JavaScript libraries to load</dd>
+     *     </dl>
      *
      * @param reply
      *            JSON response object from the server
@@ -336,12 +337,14 @@ var Tapestry = {
 
         Tapestry.ScriptManager.addStylesheets(reply.stylesheets);
 
-        Tapestry.ScriptManager.addScripts(reply.scripts, function () {
-            /* Let the caller do its thing first (i.e., modify the DOM). */
-            callback.call(this);
+        require(["core/pageinit"], function (pageinit) {
+            pageinit.loadScripts(reply.scripts,
+                    function () {
+                        /* Let the caller do its thing first (i.e., modify the DOM). */
+                        callback && callback.call(null);
 
-            /* And handle the scripts after the DOM is updated. */
-            Tapestry.executeInits(reply.inits);
+                        pageinit.initialize(reply.inits, Tapestry.onDomLoadedCallback);
+                    });
         });
     },
 
@@ -1964,94 +1967,12 @@ Tapestry.FormInjector = Class.create({
     }
 });
 
+// Used to manage Scripts and Stylesheets; now just Stylesheets, likely to go away soon.
 Tapestry.ScriptManager = {
 
-    initialize: function () {
-
-        /*
-         * Check to see if document.scripts is supported; if not (for example,
-         * FireFox), we can fake it.
-         */
-        this.emulated = false;
-
-        if (!document.scripts) {
-            this.emulated = true;
-
-            document.scripts = new Array();
-
-            $$('script').each(function (s) {
-                document.scripts.push(s);
-            });
-        }
-    },
-
-    loadScript: function (scriptURL, callback) {
-        /* IE needs the type="text/javascript" as well. */
-        var element = new Element('script', {
-            src: scriptURL,
-            type: 'text/javascript'
-        });
-
-        $$("head").first().insert({
-            bottom: element
-        });
-
-        if (this.emulated)
-            document.scripts.push(element);
-
-        if (Prototype.Browser.IE) {
-            var loaded = false;
-
-            element.onreadystatechange = function () {
-                /* IE may fire either 'loaded' or 'complete', or possibly both. */
-                if (!loaded && this.readyState == 'loaded'
-                        || this.readyState == 'complete') {
-                    loaded = true;
-
-                    callback.call(this);
-                }
-            };
-
-            return;
-        }
-
-        /* Safari, Firefox, etc. are easier. */
-
-        element.onload = callback.bindAsEventListener(this);
-    },
-
     rebuildURLIfIE: Prototype.Browser.IE ? Tapestry.rebuildURL : T5._.identity,
 
     /**
-     * Add scripts, as needed, to the document, then waits for them all to load,
-     * and finally, calls the callback function.
-     *
-     * @param scripts
-     *            Array of scripts to load
-     * @param callback
-     *            invoked after scripts are loaded
-     */
-    addScripts: function (scripts, callback) {
-
-        var _ = T5._;
-
-        var loaded = _(document.scripts).chain().pluck("src").without("").map(this.rebuildURLIfIE).value();
-
-        var self = this;
-
-        var topCallback = _(scripts).chain().map(Tapestry.rebuildURL).difference(loaded).reverse().reduce(
-                function (nextCallback, scriptURL) {
-                    return function () {
-                        self.loadScript(scriptURL, nextCallback);
-                    }
-                }, callback).value();
-
-        // Kick if off with the callback that loads the first script:
-
-        topCallback.call(this);
-    },
-
-    /**
      * Adds stylesheets to the document. Each element in stylesheets is an object with two keys: href (the URL to the CSS file) and
      * (optionally) media.
      * @param stylesheets