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/10/20 02:11:23 UTC

[1/3] git commit: Change the SPI such that returning false from an event handler will implicitly stop the event (an idea present in jQuery but not Prototype)

Updated Branches:
  refs/heads/5.4-js-rewrite 0198b8375 -> d93feaaa8


Change the SPI such that returning false from an event handler will implicitly stop the event (an idea present in jQuery but not Prototype)


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

Branch: refs/heads/5.4-js-rewrite
Commit: d93feaaa8c72d7ba40e4134ab20c7b7fdab2761a
Parents: 2c6791d
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Oct 19 17:11:03 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Oct 19 17:11:03 2012 -0700

----------------------------------------------------------------------
 .../META-INF/modules/core/exceptionframe.coffee    |    4 +-
 .../META-INF/modules/core/form-fragment.coffee     |    4 +-
 .../META-INF/modules/core/forms.coffee             |    7 ++---
 .../coffeescript/META-INF/modules/core/spi.coffee  |   12 +++++++++-
 .../integration/app1/pages/test-spi.coffee         |   17 ++++++++-------
 5 files changed, 27 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/d93feaaa/tapestry-core/src/main/coffeescript/META-INF/modules/core/exceptionframe.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/exceptionframe.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/exceptionframe.coffee
index a44cdcc..c990df5 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/exceptionframe.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/exceptionframe.coffee
@@ -29,10 +29,10 @@ define ["core/spi", "core/builder", "_"],
       iframeDocument.write content
       iframeDocument.close()
 
-    clear = (event) ->
-      event.stop()
+    clear = ->
       write ""
       container.hide()
+      return false
 
     # Called after the window has resized to adjust the size of the iframe.
     resize = ->

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/d93feaaa/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
index d6415a5..319cac2 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
@@ -47,14 +47,14 @@ define ["_", "core/spi", "core/events", "core/compat/tapestry"],
     # from the old style (the FormFragment class as controller) to the new style (DOM events and
     # top-level event handlers).
     spi.onDocument events.formfragment.changeVisibility, SELECTOR, (event) ->
-        event.stop()
-
         makeVisible = event.memo.visible
 
         this[if makeVisible then "show" else "hide"]()
 
         this.trigger events.element[if makeVisible then "didShow" else "didHide"]
 
+        return false
+
     # Initializes a trigger for a FormFragment
     #
     # * spec.triggerId - id of checkbox or radio button

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/d93feaaa/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
index f069401..8bddbf9 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
@@ -45,7 +45,7 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
 
       hidden.setValue value
 
-    defaultValidateAndSubmit = (event) ->
+    defaultValidateAndSubmit = ->
 
       if ((this.getAttribute "data-validate") is "submit") and
          (not this.getAttribute SKIP_VALIDATION)
@@ -64,8 +64,7 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
 
         if memo.error
           clearSubmittingHidden this
-          event.stop()
-          return
+          return false
 
       # Allow certain types of elements to do last-moment set up. Basically, this is for
       # FormFragment, or similar, to make their hidden field enabled or disabled to match
@@ -76,8 +75,8 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
       # Othertimes we want to stop here and let the `events.form.processSubmit`
       # handler take it from here.
       if isPreventSubmission this
-        event.stop()
         this.trigger events.form.processSubmit, this
+        return false
 
       # Otherwise, the event is good, there are no validation problems, let the normal processing commence.
       return

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/d93feaaa/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
index e5106ea..b3850ac 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
@@ -124,6 +124,10 @@ define ["_", "prototype"], (_) ->
   # * match - selector to match bubbled elements, or null
   # * handler - event handler function to invoke; it will be passed an `EventWrapper` instance as the first parameter,
   #   and the memo as the second parameter. `this` will be the `ElementWrapper` for the matched element.
+  #
+  # Event handlers may return false to stop event propogation; this prevents an event from bubbling up, and
+  # prevents any browser default behavior from triggering.  This is often easier than accepting the `EventWrapper`
+  # object as the first parameter and invoking `stop()`.
   class EventHandler
     constructor: (elements, eventNames, match, handler) ->
       throw new Error "No event handler was provided." unless handler?
@@ -133,7 +137,13 @@ define ["_", "prototype"], (_) ->
         elementWrapper = new ElementWrapper prototypeEvent.findElement()
         eventWrapper = new EventWrapper prototypeEvent
 
-        handler.call elementWrapper, eventWrapper, eventWrapper.memo
+        result = handler.call elementWrapper, eventWrapper, eventWrapper.memo
+
+        # If an event handler returns exactly false, then stop the event.
+        if result is false
+          prototypeEvent.stop()
+
+        return
 
       # Prototype Event.Handler instances
       @protoHandlers = []

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/d93feaaa/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee
index 3c04fce..6b2f2c3 100644
--- a/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee
+++ b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee
@@ -20,9 +20,9 @@ require ["core/spi"], (spi) ->
     # Remember that Prototype will never trigger a native event, just a
     # custom event, so we create a custom event here.
     # NOTE: support for native events was added later.
-    eh = container.on "x:click", "a", (event) ->
-      event.stop()
+    eh = container.on "x:click", "a", ->
       clicks++
+      return false
 
     button.trigger "x:click"
 
@@ -48,9 +48,9 @@ require ["core/spi"], (spi) ->
     container = spi "spi-eventelement"
     button = container.findFirst "a"
 
-    eh = container.on "click", "a", (event) ->
-      event.stop()
+    eh = container.on "click", "a", ->
       clicks++
+      return false
 
     button.trigger "click"
 
@@ -65,9 +65,9 @@ require ["core/spi"], (spi) ->
     primary = container.findFirst "a.btn-primary"
     secondary = container.findFirst "a[data-use=secondary]"
 
-    eh = container.on "x:click", "a.btn-primary", (event) ->
-      event.stop()
+    eh = container.on "x:click", "a.btn-primary", ->
       clicks++
+      return false
 
     primary.trigger "x:click"
 
@@ -84,11 +84,12 @@ require ["core/spi"], (spi) ->
     container = spi "spi-eventelement"
     primary = container.findFirst "a.btn-primary"
 
-    eh = container.on "x:click", "a.btn-primary", (event) ->
-      event.stop()
+    eh = container.on "x:click", "a.btn-primary", ->
 
       strictEqual this.element, primary.element, "this should be the wrapper for element that was matched"
 
+      return false
+
     primary.trigger "x:click"
 
     eh.stop()