Fw: Communication between Tapestry and Angular(inside of an iframe)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Fw: Communication between Tapestry and Angular(inside of an iframe)

Hristo Stoyanov
Hello everyone!

I have been brought onboard to an Tapestry project(never heard of it before) and I would like to create a 'communication channel' between the tapestry and an iframe(displaying Angular application).

Both projects are on the same domain and the Angular app inside the iframe works as expected, but I would like to send data to the Angular project and vice versa. 

I found out that you can communicate between parent window and iframe by using cross-document messaging (https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage)

My question is:
How could I possibly make an event listener inside Java? The javascript for doing so should look something like this "window.document.addEventListener("myEvent", 'JS function to handle', false);"
And also how to actually call a method inside Java from the JavaScript code.

I asked here and there and nobody was able to help me with that so far...
I will be very thankful if you can help me with that.

There is a small image that can helpfully help you understand my idea:
">

Thank you in advance!


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Fw: Communication between Tapestry and Angular(inside of an iframe)

Dmitry Gusev
Hi Hristo,

This mailing list isn't allowing attachments, we cannot see it, so it's a
bit unclear what you need.

> How could I possibly make an event listener inside Java?

What events should the listener listen to, is it a client-side or
server-side event?

To call Java methods from JS you usually build an API endpoint, i.e. with
Resteasy or GraphQL.
I presume you'll need access to the Tapestry context, you can use on of the
ready-to-use integrations,
e.g. http://www.tynamo.org/tapestry-resteasy+guide/

Alternatively, if you need to call a Tapestry page/component and
potentially render some markup in response
you can generate an event link from the page/component and pass it to JS
via the initialiser, for example, in your page/component class:

    @Inject ComponentResources componentResources;

    public void afterRender()
    {
        JSONObject json = new JSONObject()

.put("myEventURL",
componentResources.createEventLink("myEvent").toRedirectURI());

        // this will invoke a client-side function with arguments
        javaScriptSupport.require("myJSModule").invoke("myInit").with(json);
    }

    @OnEvent("myEvent")
    public void onMyEvent() {
        // TODO Handle event on the server-side
    }

and then create META-INF/modules/myJSModule.js and put the following code
to receive the call on client-side:

(function () {

    // you can define module dependencies if you need them
    define(["t5/core/ajax"], function (ajax) {

        function init(spec) {
            // read argument here, and when needed use the URL to call it
with `ajax`
            alert(spec.myEventURL);
        }

        return {
            init: init
        };
    });
})();




On Fri, Oct 9, 2020 at 8:51 AM Hristo Stoyanov <[hidden email]>
wrote:

> Hello everyone!
>
> I have been brought onboard to an Tapestry project(never heard of it
> before) and I would like to create a 'communication channel' between the
> tapestry and an iframe(displaying Angular application).
>
> Both projects are on the same domain and the Angular app inside the iframe
> works as expected, but I would like to send data to the Angular project and
> vice versa.
>
> I found out that you can communicate between parent window and iframe by
> using cross-document messaging (
> https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage)
>
> My question is:
> How could I possibly make an event listener inside Java? The javascript
> for doing so should look something like this
> "window.document.addEventListener("myEvent", 'JS function to handle',
> false);"
> And also how to actually call a method inside Java from the JavaScript
> code.
>
> I asked here and there and nobody was able to help me with that so far...
> I will be very thankful if you can help me with that.
>
> There is a small image that can helpfully help you understand my idea:
> [image: Inline image]
>
> Thank you in advance!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]



--
Dmitry Gusev

AnjLab Team
http://anjlab.com
Reply | Threaded
Open this post in threaded view
|

Re: Fw: Communication between Tapestry and Angular(inside of an iframe)

Thiago H de Paula Figueiredo
On Fri, Oct 9, 2020 at 7:55 AM Dmitry Gusev <[hidden email]> wrote:

> Hi Hristo,
>

Hello, everyone!


> > How could I possibly make an event listener inside Java?
>

Both options provided by Dmitry are good, but I'd prefer the Tapestry-only
(i.e. not REST framework needed) one.

If you're using Tapestry 5.4.2 or later, you don't even need to create and
pass the event URL to JavaScript. Check the documentation at
https://tapestry.apache.org/ajax-and-zones.html#AjaxandZones-Invokingserver-sideeventhandlermethodsfromJavaScript.
If you're not using zones,  dom.getEventUrl(eventName, null)  will give you
the URL for an event name in the page class.

>
> What events should the listener listen to, is it a client-side or
> server-side event?
>
> To call Java methods from JS you usually build an API endpoint, i.e. with
> Resteasy or GraphQL.
> I presume you'll need access to the Tapestry context, you can use on of the
> ready-to-use integrations,
> e.g. http://www.tynamo.org/tapestry-resteasy+guide/
>
> Alternatively, if you need to call a Tapestry page/component and
> potentially render some markup in response
> you can generate an event link from the page/component and pass it to JS
> via the initialiser, for example, in your page/component class:
>
>     @Inject ComponentResources componentResources;
>
>     public void afterRender()
>     {
>         JSONObject json = new JSONObject()
>
> .put("myEventURL",
> componentResources.createEventLink("myEvent").toRedirectURI());
>
>         // this will invoke a client-side function with arguments
>
> javaScriptSupport.require("myJSModule").invoke("myInit").with(json);
>     }
>
>     @OnEvent("myEvent")
>     public void onMyEvent() {
>         // TODO Handle event on the server-side
>     }
>
> and then create META-INF/modules/myJSModule.js and put the following code
> to receive the call on client-side:
>
> (function () {
>
>     // you can define module dependencies if you need them
>     define(["t5/core/ajax"], function (ajax) {
>
>         function init(spec) {
>             // read argument here, and when needed use the URL to call it
> with `ajax`
>             alert(spec.myEventURL);
>         }
>
>         return {
>             init: init
>         };
>     });
> })();
>
>
>
>
> On Fri, Oct 9, 2020 at 8:51 AM Hristo Stoyanov
> <[hidden email]>
> wrote:
>
> > Hello everyone!
> >
> > I have been brought onboard to an Tapestry project(never heard of it
> > before) and I would like to create a 'communication channel' between the
> > tapestry and an iframe(displaying Angular application).
> >
> > Both projects are on the same domain and the Angular app inside the
> iframe
> > works as expected, but I would like to send data to the Angular project
> and
> > vice versa.
> >
> > I found out that you can communicate between parent window and iframe by
> > using cross-document messaging (
> > https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage)
> >
> > My question is:
> > How could I possibly make an event listener inside Java? The javascript
> > for doing so should look something like this
> > "window.document.addEventListener("myEvent", 'JS function to handle',
> > false);"
> > And also how to actually call a method inside Java from the JavaScript
> > code.
> >
> > I asked here and there and nobody was able to help me with that so far...
> > I will be very thankful if you can help me with that.
> >
> > There is a small image that can helpfully help you understand my idea:
> > [image: Inline image]
> >
> > Thank you in advance!
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
>
>
>
> --
> Dmitry Gusev
>
> AnjLab Team
> http://anjlab.com
>


--
Thiago
Reply | Threaded
Open this post in threaded view
|

Re: Communication between Tapestry and Angular(inside of an iframe)

Hristo Stoyanov
 Thank you everyone,
I made an adjustment to the project and now the Angular project will not need to call tapestry code anymore. 

Thank you for the good help! :) 

Best regards
    On Wednesday, October 21, 2020, 04:08:16 PM GMT+2, Thiago H. de Paula Figueiredo <[hidden email]> wrote:  
 
 On Fri, Oct 9, 2020 at 7:55 AM Dmitry Gusev <[hidden email]> wrote:

> Hi Hristo,
>

Hello, everyone!


> > How could I possibly make an event listener inside Java?
>

Both options provided by Dmitry are good, but I'd prefer the Tapestry-only
(i.e. not REST framework needed) one.

If you're using Tapestry 5.4.2 or later, you don't even need to create and
pass the event URL to JavaScript. Check the documentation at
https://tapestry.apache.org/ajax-and-zones.html#AjaxandZones-Invokingserver-sideeventhandlermethodsfromJavaScript.
If you're not using zones,  dom.getEventUrl(eventName, null)  will give you
the URL for an event name in the page class.

>
> What events should the listener listen to, is it a client-side or
> server-side event?
>
> To call Java methods from JS you usually build an API endpoint, i.e. with
> Resteasy or GraphQL.
> I presume you'll need access to the Tapestry context, you can use on of the
> ready-to-use integrations,
> e.g. http://www.tynamo.org/tapestry-resteasy+guide/
>
> Alternatively, if you need to call a Tapestry page/component and
> potentially render some markup in response
> you can generate an event link from the page/component and pass it to JS
> via the initialiser, for example, in your page/component class:
>
>    @Inject ComponentResources componentResources;
>
>    public void afterRender()
>    {
>        JSONObject json = new JSONObject()
>
> .put("myEventURL",
> componentResources.createEventLink("myEvent").toRedirectURI());
>
>        // this will invoke a client-side function with arguments
>
> javaScriptSupport.require("myJSModule").invoke("myInit").with(json);
>    }
>
>    @OnEvent("myEvent")
>    public void onMyEvent() {
>        // TODO Handle event on the server-side
>    }
>
> and then create META-INF/modules/myJSModule.js and put the following code
> to receive the call on client-side:
>
> (function () {
>
>    // you can define module dependencies if you need them
>    define(["t5/core/ajax"], function (ajax) {
>
>        function init(spec) {
>            // read argument here, and when needed use the URL to call it
> with `ajax`
>            alert(spec.myEventURL);
>        }
>
>        return {
>            init: init
>        };
>    });
> })();
>
>
>
>
> On Fri, Oct 9, 2020 at 8:51 AM Hristo Stoyanov
> <[hidden email]>
> wrote:
>
> > Hello everyone!
> >
> > I have been brought onboard to an Tapestry project(never heard of it
> > before) and I would like to create a 'communication channel' between the
> > tapestry and an iframe(displaying Angular application).
> >
> > Both projects are on the same domain and the Angular app inside the
> iframe
> > works as expected, but I would like to send data to the Angular project
> and
> > vice versa.
> >
> > I found out that you can communicate between parent window and iframe by
> > using cross-document messaging (
> > https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage)
> >
> > My question is:
> > How could I possibly make an event listener inside Java? The javascript
> > for doing so should look something like this
> > "window.document.addEventListener("myEvent", 'JS function to handle',
> > false);"
> > And also how to actually call a method inside Java from the JavaScript
> > code.
> >
> > I asked here and there and nobody was able to help me with that so far...
> > I will be very thankful if you can help me with that.
> >
> > There is a small image that can helpfully help you understand my idea:
> > [image: Inline image]
> >
> > Thank you in advance!
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
>
>
>
> --
> Dmitry Gusev
>
> AnjLab Team
> http://anjlab.com
>


--
Thiago
 
Reply | Threaded
Open this post in threaded view
|

Re: Communication between Tapestry and Angular(inside of an iframe)

Emmanuel Sowah
That is a very good decision, Hristo. Probably even better is to ask your
company to re-write all that Tapestry shit in Angular. Tapestry is so
arcane and has a very few cult-like followers. Even the founder, Howard
Lewis Ship, realized how bad the framework is and is now using Angular on
all his projects.He abandoned this ship long ago.

On Sun, Oct 25, 2020 at 11:46 AM Hristo Stoyanov
<[hidden email]> wrote:

>  Thank you everyone,
> I made an adjustment to the project and now the Angular project will not
> need to call tapestry code anymore.
>
> Thank you for the good help! :)
>
> Best regards
>     On Wednesday, October 21, 2020, 04:08:16 PM GMT+2, Thiago H. de Paula
> Figueiredo <[hidden email]> wrote:
>
>  On Fri, Oct 9, 2020 at 7:55 AM Dmitry Gusev <[hidden email]>
> wrote:
>
> > Hi Hristo,
> >
>
> Hello, everyone!
>
>
> > > How could I possibly make an event listener inside Java?
> >
>
> Both options provided by Dmitry are good, but I'd prefer the Tapestry-only
> (i.e. not REST framework needed) one.
>
> If you're using Tapestry 5.4.2 or later, you don't even need to create and
> pass the event URL to JavaScript. Check the documentation at
>
> https://tapestry.apache.org/ajax-and-zones.html#AjaxandZones-Invokingserver-sideeventhandlermethodsfromJavaScript
> .
> If you're not using zones,  dom.getEventUrl(eventName, null)  will give you
> the URL for an event name in the page class.
>
> >
> > What events should the listener listen to, is it a client-side or
> > server-side event?
> >
> > To call Java methods from JS you usually build an API endpoint, i.e. with
> > Resteasy or GraphQL.
> > I presume you'll need access to the Tapestry context, you can use on of
> the
> > ready-to-use integrations,
> > e.g. http://www.tynamo.org/tapestry-resteasy+guide/
> >
> > Alternatively, if you need to call a Tapestry page/component and
> > potentially render some markup in response
> > you can generate an event link from the page/component and pass it to JS
> > via the initialiser, for example, in your page/component class:
> >
> >    @Inject ComponentResources componentResources;
> >
> >    public void afterRender()
> >    {
> >        JSONObject json = new JSONObject()
> >
> > .put("myEventURL",
> > componentResources.createEventLink("myEvent").toRedirectURI());
> >
> >        // this will invoke a client-side function with arguments
> >
> > javaScriptSupport.require("myJSModule").invoke("myInit").with(json);
> >    }
> >
> >    @OnEvent("myEvent")
> >    public void onMyEvent() {
> >        // TODO Handle event on the server-side
> >    }
> >
> > and then create META-INF/modules/myJSModule.js and put the following code
> > to receive the call on client-side:
> >
> > (function () {
> >
> >    // you can define module dependencies if you need them
> >    define(["t5/core/ajax"], function (ajax) {
> >
> >        function init(spec) {
> >            // read argument here, and when needed use the URL to call it
> > with `ajax`
> >            alert(spec.myEventURL);
> >        }
> >
> >        return {
> >            init: init
> >        };
> >    });
> > })();
> >
> >
> >
> >
> > On Fri, Oct 9, 2020 at 8:51 AM Hristo Stoyanov
> > <[hidden email]>
> > wrote:
> >
> > > Hello everyone!
> > >
> > > I have been brought onboard to an Tapestry project(never heard of it
> > > before) and I would like to create a 'communication channel' between
> the
> > > tapestry and an iframe(displaying Angular application).
> > >
> > > Both projects are on the same domain and the Angular app inside the
> > iframe
> > > works as expected, but I would like to send data to the Angular project
> > and
> > > vice versa.
> > >
> > > I found out that you can communicate between parent window and iframe
> by
> > > using cross-document messaging (
> > > https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage)
> > >
> > > My question is:
> > > How could I possibly make an event listener inside Java? The javascript
> > > for doing so should look something like this
> > > "window.document.addEventListener("myEvent", 'JS function to handle',
> > > false);"
> > > And also how to actually call a method inside Java from the JavaScript
> > > code.
> > >
> > > I asked here and there and nobody was able to help me with that so
> far...
> > > I will be very thankful if you can help me with that.
> > >
> > > There is a small image that can helpfully help you understand my idea:
> > > [image: Inline image]
> > >
> > > Thank you in advance!
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [hidden email]
> > > For additional commands, e-mail: [hidden email]
> >
> >
> >
> > --
> > Dmitry Gusev
> >
> > AnjLab Team
> > http://anjlab.com
> >
>
>
> --
> Thiago
>