Re: notify embedded components
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 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 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-unsubscr...@tapestry.apache.org >>> For additional commands, e-mail: users-h...@tapestry.apache.org >>> >>> >>> > > - > > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org > For additional commands, e-mail: users-h...@tapestry.apache.org > > -- Dmitry Gusev AnjLab Team http://anjlab.com
Re: notify embedded components
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 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 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-unsubscr...@tapestry.apache.org >>> For additional commands, e-mail: users-h...@tapestry.apache.org >>> >>> > > > - > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org > For additional commands, e-mail: users-h...@tapestry.apache.org >
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 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-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org - To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org
RE: Redirecting to a page from a MethodAdvice implementation
You could get rid of the unnecessary interface using @InjectService or a marker annotation eg: binder.bind(MethodAdvice.class, ExceptionDisplayMethodAdviceImpl.class).withId("foo"); ... @InjectService("foo") private MethodAdvice advice; More info here: http://tapestry.apache.org/defining-tapestry-ioc-services.html
RE: Redirecting to a page from a MethodAdvice implementation
I'm not sure if the following is what the hints above meant, however it works, in case it can be useful to others. Thanks for the help. - package myproject.tapestry.services; import org.apache.tapestry5.plastic.MethodAdvice; public interface ExceptionDisplayMethodAdvice extends MethodAdvice { // No code in this subclass. } - package myproject.tapestry.services; public class ExceptionDisplayMethodAdviceImpl implements ExceptionDisplayMethodAdvice { @Inject private PageRenderLinkSource pageRenderLinkSource; @Inject private Response response; /** * @see MethodAdvice#advise(MethodInvocation) */ @Override public void advise(MethodInvocation invocation) { System.err.println(this.pageRenderLinkSource + ";" + this.response); MethodInvocation result = invocation.proceed(); if (result.didThrowCheckedException()) { Exception exception = result.getCheckedException(Exception.class); Exception.printStackTrace(); // Redirect to the exception page : result.setCheckedException(null); Link exceptionDisplayPage = this.pageRenderLinkSource.createPageRenderLink(ExceptionDisplayPage.class); try { this.response.sendRedirect(exceptionDisplayPage); } catch (IOException e) { e.printStackTrace(); } } } } - public class ExceptionDisplayWorker implements ComponentClassTransformWorker2 { @Inject private ExceptionDisplayMethodAdvice advice; @Override public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) { for (PlasticMethod method : plasticClass.getMethodsWithAnnotation(ExceptionDisplay.class)) { method.addAdvice(this.advice); } } } - package myproject.tapestry.services; public class MyProjectModule { public static void bind(ServiceBinder binder) { binder.bind(ExceptionDisplayMethodAdvice.class, ExceptionDisplayMethodAdviceImpl.class); } } -
Re: WebSocket for tapestry
Ok, thanks for reporting, I've raised an issue here https://github.com/uklance/tapestry-atmosphere/issues/22