Repository: tapestry-5 Updated Branches: refs/heads/master 6b4ca30b8 -> 29f38edba
TAP5-2225 Create client-side API to call a component's event handler methods Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/29f38edb Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/29f38edb Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/29f38edb Branch: refs/heads/master Commit: 29f38edbaf1a083906a52486f34da2137cc0a47f Parents: 6b4ca30 Author: Thiago H. de Paula Figueiredo <[email protected]> Authored: Sun Mar 19 16:11:04 2017 -0300 Committer: Thiago H. de Paula Figueiredo <[email protected]> Committed: Sun Mar 19 16:11:04 2017 -0300 ---------------------------------------------------------------------- .../META-INF/modules/t5/core/ajax.coffee | 5 ++ .../org/apache/tapestry5/t5-core-dom.coffee | 5 ++ .../src/test/app1/PublishEventDemo.tml | 75 ++++++++++++++++++++ .../components/PublishEventDemoComponent.java | 4 +- .../components/PublishEventDemoComponent2.java | 4 +- .../app1/pages/PublishEventDemo.java | 4 +- .../META-INF/assets/PublishEventDemo.js | 27 +++++-- 7 files changed, 111 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/29f38edb/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/ajax.coffee ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/ajax.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/ajax.coffee index 0237ebb..f3695a5 100644 --- a/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/ajax.coffee +++ b/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/ajax.coffee @@ -18,6 +18,8 @@ # `options`. # # * options.method - "post", "get", etc., default: "post". +# * options.element - if provided, the URL will be treated as a server-side event name +# and the actual URL to be used will be obtained from dom.getEventUrl(url, element) # * options.contentType - request content, defaults to "application/x-www-form-urlencoded" # * options.data - optional, additional key/value pairs (for the default content type) # * options.success - handler to invoke on success. Passed the ResponseWrapper object. @@ -42,6 +44,9 @@ define ["./pageinit", "./dom", "./exception-frame", "./console", "underscore"], options.complete() return + + if options.hasOwnProperty 'element' + url = dom.getEventUrl(url, options.element) newOptions = _.extend {}, options, http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/29f38edb/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee b/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee index 9b975d4..1f07d96 100644 --- a/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee +++ b/tapestry-core/src/main/preprocessed-coffeescript/org/apache/tapestry5/t5-core-dom.coffee @@ -922,6 +922,11 @@ define ["underscore", "./utils", "./events", "jquery"], throw 'dom.getEventUrl: the eventName parameter should be a string' eventName = eventName.toLowerCase() + +#if jquery + if element instanceof jQuery + element = element[0]; +#end if element is null element = document.getElementsByTagName('body')[0] http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/29f38edb/tapestry-core/src/test/app1/PublishEventDemo.tml ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/app1/PublishEventDemo.tml b/tapestry-core/src/test/app1/PublishEventDemo.tml index 43c2bd7..467665c 100644 --- a/tapestry-core/src/test/app1/PublishEventDemo.tml +++ b/tapestry-core/src/test/app1/PublishEventDemo.tml @@ -4,4 +4,79 @@ <t:PublishEventDemoComponent/> <t:PublishEventDemoComponent2/> </div> + <p> + Results: + </p> + <table class="table table-striped"> + <thead> + <tr> + <td>Component id</td> + <td>Event</td> + <td>Expected answer</td> + <td>Answer</td> + </tr> + </thead> + <tbody> + <tr> + <td>(no element)</td> + <td>action</td> + <td>pageAction</td> + <td id="nullAction"></td> + </tr> + <tr> + <td>(no element)</td> + <td>answer</td> + <td>pageAnswer</td> + <td id="nullAnswer"></td> + </tr> + <tr> + <td>page</td> + <td>action</td> + <td>pageAction</td> + <td id="pageAction"></td> + </tr> + <tr> + <td>page</td> + <td>answer</td> + <td>pageAnswer</td> + <td id="pageAnswer"></td> + </tr> + <tr> + <td>componentParagraph</td> + <td>action</td> + <td>pageAction</td> + <td id="componentParagraphAction"></td> + </tr> + <tr> + <td>componentParagraph</td> + <td>answer</td> + <td>pageAnswer</td> + <td id="componentParagraphAnswer"></td> + </tr> + <tr> + <td>componentParagraph2</td> + <td>action</td> + <td>componentAction2</td> + <td id="componentParagraph2Action"></td> + </tr> + <tr> + <td>componentParagraph2</td> + <td>answer</td> + <td>componentAnswer2</td> + <td id="componentParagraph2Answer"></td> + </tr> + <tr> + <td>componentParagraph3</td> + <td>action</td> + <td>componentAction2</td> + <td id="componentParagraph3Action"></td> + </tr> + <tr> + <td>componentParagraph3</td> + <td>answer</td> + <td>componentAnswer2</td> + <td id="componentParagraph3Answer"></td> + </tr> + </tbody> + </table> </t:border> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/29f38edb/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent.java index b8054f9..e402cbc 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent.java @@ -20,13 +20,13 @@ public class PublishEventDemoComponent @OnEvent("answer") @PublishEvent JSONObject answer() { - return new JSONObject("origin", "component"); + return new JSONObject("origin", "componentAction"); } @PublishEvent JSONObject onAction() { - return new JSONObject("origin", "component"); + return new JSONObject("origin", "componentAnswer"); } } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/29f38edb/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent2.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent2.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent2.java index 7771557..df987ad 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent2.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/PublishEventDemoComponent2.java @@ -20,13 +20,13 @@ public class PublishEventDemoComponent2 @PublishEvent JSONObject onAction() { - return new JSONObject("origin", "component"); + return new JSONObject("origin", "componentAction2"); } @OnEvent("answer") @PublishEvent JSONObject answer() { - return new JSONObject("origin", "component"); + return new JSONObject("origin", "componentAnswer2"); } } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/29f38edb/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/PublishEventDemo.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/PublishEventDemo.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/PublishEventDemo.java index 1876de5..9400d49 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/PublishEventDemo.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/PublishEventDemo.java @@ -37,13 +37,13 @@ public class PublishEventDemo @PublishEvent JSONObject onAction() { - return new JSONObject("origin", "page"); + return new JSONObject("origin", "pageAction"); } @OnEvent("answer") @PublishEvent JSONObject answer() { - return new JSONObject("origin", "page"); + return new JSONObject("origin", "pageAnswer"); } } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/29f38edb/tapestry-core/src/test/resources/META-INF/assets/PublishEventDemo.js ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/resources/META-INF/assets/PublishEventDemo.js b/tapestry-core/src/test/resources/META-INF/assets/PublishEventDemo.js index 20c0406..d10eab0 100644 --- a/tapestry-core/src/test/resources/META-INF/assets/PublishEventDemo.js +++ b/tapestry-core/src/test/resources/META-INF/assets/PublishEventDemo.js @@ -1,11 +1,24 @@ require(["t5/core/dom", "t5/core/ajax", "jquery"], function (dom, ajax, $) { - $(document).ready(function() { - console.log('dom.getEventURL() : ' + dom.getEventUrl('answer', document.getElementById("page"))); - console.log('dom.getEventURL() 1 : ' + dom.getEventUrl('answer', document.getElementById("componentParagraph"))); - console.log('dom.getEventURL() 2 : ' + dom.getEventUrl('answer', document.getElementById("componentParagraph2"))); - console.log('dom.getEventURL() 3 : ' + dom.getEventUrl('answer', document.getElementById("componentParagraph3"))); - }); - + function makeAjaxCall(eventName, eventElement, outputElement) { + ajax('action', { + element: eventElement, + success: function(response) { + outputElement.innerHTML = response.json.origin; + } + }); + } + + $('tbody tr').each(function() { + var td = $(this).find('td'); + var eventName = td[1].innerHTML; + var elementId = td[0].innerHTML; + var eventElement = null; + if (elementId != '(no element)') { + eventElement = $('#' + elementId); + } + makeAjaxCall(eventName, eventElement, td[3]); + }); + });
