You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Paul Stanton <pa...@mapshed.com.au> on 2014/06/20 06:50:15 UTC

Re: notify embedded components

This seems to work, but i'm not sure i trust it!

anyone have a better idea? It would be great to be able to subscribe to 
an event thrown by the container...

MyPage {
     @Persist
     Private List<Runnable> listeners;

     void setupRender()    {
         listeners = new ArrayList<>();
     }

     public void addListener(Runnable r)    {
         listeners.add(r);
     }

     void onSomeEvent()    {
         doSomeProcessing();
         for (Runnable l : listeners)
             l.run();
     }
}

MyEmbeddedComponent {
     @Inject
     private ComponentResources resources;

     void setupRender()    {
         ((MyPage) resources.getPage()).addListener(new Runnable()     {
             public void run()            {
                 listenerMethod();
             }
         });
     }

     void listenerMethod()    {
         doSomething();
     }
}

On 23/03/2013 3:36 AM, Howard Lewis Ship wrote:
> ive tought this woud be a cool feature, but it does not exist yet.
>
> On Thursday, March 21, 2013, Paul Stanton wrote:
>
>> Hi all,
>>
>> Is there a way to trigger an event in a container (Page) which notifies
>> embedded components?
>>
>> I'm hoping to do something like this (pseudo code) :
>>
>> Page
>> {
>>      void onSomeEvent()
>>      {
>>          notifyListeners("**SomethingHappened");
>>      }
>> }
>>
>> EmbeddedComponent
>> {
>>      void setupRender()
>>      {
>>          page.listenTo("**SomethingHappened");
>>      }
>>
>>      void onSomethingHappened()
>>      {
>>          // called when triggered by page
>>      }
>> }
>>
>> Help appreciated, thanks, paul.
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: notify embedded components

Posted by Geoff Callender <ge...@gmail.com>.
Very cute. It's something akin to GWT's event buses, but server-side.

A less cute alternative, but one which and has served my needs to date and is straight-forward, is to have the container call a method on each interested component. You can see it in the following example. Page Persons has several event handlers that call list.doChangeOfSelectedPerson().

	http://jumpstart.doublenegative.com.au/jumpstart7/together/ajaxcomponentscrud/persons

Some extra benefits are that it doesn't need @Persist, and each piece has the opportunity to set up or pass parameters.

Cheers,

Geoff

On 20 Jun 2014, at 2:50 pm, Paul Stanton <pa...@mapshed.com.au> wrote:

> This seems to work, but i'm not sure i trust it!
> 
> anyone have a better idea? It would be great to be able to subscribe to an event thrown by the container...
> 
> MyPage {
>    @Persist
>    Private List<Runnable> listeners;
> 
>    void setupRender()    {
>        listeners = new ArrayList<>();
>    }
> 
>    public void addListener(Runnable r)    {
>        listeners.add(r);
>    }
> 
>    void onSomeEvent()    {
>        doSomeProcessing();
>        for (Runnable l : listeners)
>            l.run();
>    }
> }
> 
> MyEmbeddedComponent {
>    @Inject
>    private ComponentResources resources;
> 
>    void setupRender()    {
>        ((MyPage) resources.getPage()).addListener(new Runnable()     {
>            public void run()            {
>                listenerMethod();
>            }
>        });
>    }
> 
>    void listenerMethod()    {
>        doSomething();
>    }
> }
> 
> On 23/03/2013 3:36 AM, Howard Lewis Ship wrote:
>> ive tought this woud be a cool feature, but it does not exist yet.
>> 
>> On Thursday, March 21, 2013, Paul Stanton wrote:
>> 
>>> Hi all,
>>> 
>>> Is there a way to trigger an event in a container (Page) which notifies
>>> embedded components?
>>> 
>>> I'm hoping to do something like this (pseudo code) :
>>> 
>>> Page
>>> {
>>>     void onSomeEvent()
>>>     {
>>>         notifyListeners("**SomethingHappened");
>>>     }
>>> }
>>> 
>>> EmbeddedComponent
>>> {
>>>     void setupRender()
>>>     {
>>>         page.listenTo("**SomethingHappened");
>>>     }
>>> 
>>>     void onSomethingHappened()
>>>     {
>>>         // called when triggered by page
>>>     }
>>> }
>>> 
>>> Help appreciated, thanks, paul.
>>> 
>>> ------------------------------**------------------------------**---------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>> 
>>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


Re: notify embedded components

Posted by Dmitry Gusev <dm...@gmail.com>.
You better trust it if it works :)

There's 3rd-party Publisher API that does similar thing:
https://github.com/anjlab/anjlab-tapestry-commons/wiki/Publisher-API

Look at the source code to see implementation details.

Publisher and subscriber doesn't have to be nested there, they can be in
different pages, here's an example of how you normally use it:

Subscriber code:

    @Inject private Publisher publisher;



    public void pageLoaded()

    {

        publisher.subscribe(ShareLink.SHARING_CHANGED_EVENT_TYPE, this);

    }



    @OnEvent(value=ShareLink.SHARING_CHANGED_EVENT_TYPE)

    public boolean onSharingChanged(long invoiceId)

    {

       //  handle event

    }

Publisher code:

        publisher.triggerEvent(SHARING_CHANGED_EVENT_TYPE, new Object[] {
invoiceId }, null);





On Fri, Jun 20, 2014 at 8:50 AM, Paul Stanton <pa...@mapshed.com.au> wrote:

> This seems to work, but i'm not sure i trust it!
>
> anyone have a better idea? It would be great to be able to subscribe to an
> event thrown by the container...
>
> MyPage {
>     @Persist
>     Private List<Runnable> listeners;
>
>     void setupRender()    {
>         listeners = new ArrayList<>();
>     }
>
>     public void addListener(Runnable r)    {
>         listeners.add(r);
>     }
>
>     void onSomeEvent()    {
>         doSomeProcessing();
>         for (Runnable l : listeners)
>             l.run();
>     }
> }
>
> MyEmbeddedComponent {
>     @Inject
>     private ComponentResources resources;
>
>     void setupRender()    {
>         ((MyPage) resources.getPage()).addListener(new Runnable()     {
>             public void run()            {
>                 listenerMethod();
>             }
>         });
>     }
>
>     void listenerMethod()    {
>         doSomething();
>
>     }
> }
>
> On 23/03/2013 3:36 AM, Howard Lewis Ship wrote:
>
>> ive tought this woud be a cool feature, but it does not exist yet.
>>
>> On Thursday, March 21, 2013, Paul Stanton wrote:
>>
>>  Hi all,
>>>
>>> Is there a way to trigger an event in a container (Page) which notifies
>>> embedded components?
>>>
>>> I'm hoping to do something like this (pseudo code) :
>>>
>>> Page
>>> {
>>>      void onSomeEvent()
>>>      {
>>>          notifyListeners("**SomethingHappened");
>>>      }
>>> }
>>>
>>> EmbeddedComponent
>>> {
>>>      void setupRender()
>>>      {
>>>          page.listenTo("**SomethingHappened");
>>>      }
>>>
>>>      void onSomethingHappened()
>>>      {
>>>          // called when triggered by page
>>>      }
>>> }
>>>
>>> Help appreciated, thanks, paul.
>>>
>>> ------------------------------**----------------------------
>>> --**---------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>>
>
> ---------------------------------------------------------------------
>
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com