Hi Vit,

I'm using a utility class to encapsulate these use cases:

/** Utility class for interacting with {@link AjaxRequestTarget}s */
> public final class AjaxTarget {
> private AjaxTarget() {
> // utility class
> }
> /**
> * Checks if there is an active {@link AjaxRequestTarget}
> *
> * @return {@code true} if there is a request target bound to the thread,
> else {@code false}
> */
> public static boolean exists() {
> return find().isPresent();
> }
> /** @return the {@link AjaxRequestTarget} bound to the thread or {@code
> null} */
> public static AjaxRequestTarget get() {
> return find().orElse(null);
> }
> /** @return the optional {@link AjaxRequestTarget} bound to the thread */
> public static Optional<AjaxRequestTarget> find() {
> return RequestCycle.get().find(AjaxRequestTarget.class);
> }
> /**
> * Adds the given components to the active request target if available.
> *
> * @param component the first component to add
> * @param otherComponents the other components to add
> * @see AjaxRequestTarget#add(org.apache.wicket.Component...)
> */
> public static void add(Component component, Component... otherComponents) {
> find().ifPresent(t -> {
> if (component != null) {
> t.add(component);
> }
> t.add(otherComponents);
> });
> }
> /**
> * Adds all components of the given class to the active request target if
> available.
> *
> * @param childCriteria the child component class
> * @param <T> the type of child component
> * @see AjaxRequestTarget#addChildren(org.apache.wicket.MarkupContainer,
> Class)
> */
> public static <T> void add(Class<T> childCriteria) {
> find().ifPresent(t -> add(t.getPage(), childCriteria));
> }
>
/**
> * Appends JavaScript to the active request target if available
> *
> * @param javaScript the script to append
> * @see AjaxRequestTarget#appendJavaScript(CharSequence)
> */
> public static void appendJavaScript(CharSequence javaScript) {
> find().ifPresent(t -> t.appendJavaScript(javaScript));
> }
> /**
> * Prepends JavaScript to the active request target if available
> *
> * @param javaScript the script to prepend
> * @see AjaxRequestTarget#prependJavaScript(CharSequence)
> */
> public static void prependJavaScript(CharSequence javaScript) {
> find().ifPresent(t -> t.prependJavaScript(javaScript));
> }

}


Best regards,

Thomas

On Sun, Apr 5, 2020 at 6:58 PM Vit Rozkovec <rozkovec...@email.cz> wrote:

> In my project it is around 40 cases of such usage. Is it little? Is it a
> lot. I don't know.
> Never mind :)
>
> Have a nice day.
> Vit
>
> On 4/5/20 11:20 AM, Sven Meier wrote:
> > Hi,
> >
> > actually it's not that common, in wicket-core and -extensions this
> > pattern is used 9 times only.
> >
> > When the RequestCycle API emerged, we decided against a specific
> > method and chose a generic one with parameter instead.
> >
> > Have fun
> > Sven
> >
> >
> > On 05.04.20 08:47, Vit Rozkovec wrote:
> >> Hi, this seems to be a frequent use case, wouldn't there be a good
> >> fit for some shorthand method?
> >>
> >> Like
> >>
> >> /getRequestCycle().onAjax(t-> {});/
> >> boolean getRequestCycle().isAjax();
> >>
> >> ?
> >>
> >> Vit
> >>
> >>
> >> On 4/4/20 11:51 PM, Sven Meier wrote:
> >>> Hi,
> >>>
> >>> you can test for the appropriate request handler:
> >>>
> >>>
> getRequestCycle().find(IPartialPageRequestHandler.class).ifPresent(target
> >>> -> /* do things on partial page update */));
> >>>
> >>> Have fun
> >>> Sven
> >>>
> >>>
> >>> On 04.04.20 23:43, Vilius Vaivada wrote:
> >>>> Hey guys,
> >>>>
> >>>> I'm pretty sure I'm missing something obvious here, but I can't
> >>>> figure out
> >>>> a simple way for a component to contribute slightly different
> >>>> Javascript
> >>>> based on whether it's being rendered for a full page load or a
> >>>> partial Ajax
> >>>> response. Any clues?
> >>>>
> >>>> Thanks a lot!
> >>>>
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> >>> For additional commands, e-mail: users-h...@wicket.apache.org
> >>>
> >>
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> > For additional commands, e-mail: users-h...@wicket.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

Reply via email to