You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ronny Pscheidl <jo...@googlemail.com> on 2015/12/04 20:15:27 UTC

behavior passing components during construction

hello,
i want to write an collapsebehavior based on bootstrap collapse (
http://getbootstrap.com/javascript/#collapse). so you have an icon or
button or waht else which will toggle a container. both components are
independent added to the page or panel.

example:

public class CollapseBehavior extends Behavior {
   private final IModel<String> targetMarkupId;

   public CollapseBehavior(Component target) {
      target.setOutputMarkupId(true);
      target.add(new CssClassAppender("collapse"));
      targetMarkupId = target.getOutputMarkupId();
   }
}


public class HomePage extends WebPage {
   public HomePage() {
      Button collapse = new Button("button");
      WebMarkupContainer target = new WebMarkupContainer("body");
      collapse.add(new CollapseBehavior(target));
      add(collapse);
      add(target);
   }
}

wicket guide says do not pass components during construction. Is there a
better way?

regards

john.j.cool

Re: behavior passing components during construction

Posted by Ronny Pscheidl <jo...@googlemail.com>.
Hi Martin,
thanks for reply. i used onComponentTag.



2015-12-05 13:43 GMT+01:00 Martin Grigorov <ma...@gmail.com>:

> Hi Ronny ,
>
> Your approach is OK.
> All that you need is to save target.getMarkupId() in a member variable for
> later use.
> Use Behavior#onComponentTag() instead of adding yet another behavior to
> write the attribute.
> On Dec 5, 2015 2:11 PM, "Ronny Pscheidl" <jo...@googlemail.com>
> wrote:
>
> > this is not the use case. collapsebehavior is set to component icon which
> > toggles the target component. the initial state of target has to be
> > collapsed. my approach is that the collapsebehavior could also set the
> > needed css class to the target component and not to add this manually to
> > the target component.
> >
> > public class CollapseBehavior extends Behavior {
> >    private final IModel<String> targetMarkupId;
> >
> >    public CollapseBehavior(Component target) {
> >       target.setOutputMarkupId(true);
> >       target.add(new CssClassAppender("collapse"));
> >       targetMarkupId = Model.of(target.getOutputMarkupId());
> >    }
> >
> >    public void onBind(Component component) {
> >       component.add(new AttributeAppender("data-target", "#" +
> > targetMarkupId));
> >       component.add(new AttributeAppender("collapse", "toggle"));
> >    }
> >    ....
> > }
> >
> > is there a way to let a behavior interact with two components?
> >
> >
> > 2015-12-04 21:57 GMT+01:00 Sebastien <se...@gmail.com>:
> >
> > > Hi, see #bind()
> > >
> >
>

Re: behavior passing components during construction

Posted by Martin Grigorov <ma...@gmail.com>.
Hi Ronny ,

Your approach is OK.
All that you need is to save target.getMarkupId() in a member variable for
later use.
Use Behavior#onComponentTag() instead of adding yet another behavior to
write the attribute.
On Dec 5, 2015 2:11 PM, "Ronny Pscheidl" <jo...@googlemail.com> wrote:

> this is not the use case. collapsebehavior is set to component icon which
> toggles the target component. the initial state of target has to be
> collapsed. my approach is that the collapsebehavior could also set the
> needed css class to the target component and not to add this manually to
> the target component.
>
> public class CollapseBehavior extends Behavior {
>    private final IModel<String> targetMarkupId;
>
>    public CollapseBehavior(Component target) {
>       target.setOutputMarkupId(true);
>       target.add(new CssClassAppender("collapse"));
>       targetMarkupId = Model.of(target.getOutputMarkupId());
>    }
>
>    public void onBind(Component component) {
>       component.add(new AttributeAppender("data-target", "#" +
> targetMarkupId));
>       component.add(new AttributeAppender("collapse", "toggle"));
>    }
>    ....
> }
>
> is there a way to let a behavior interact with two components?
>
>
> 2015-12-04 21:57 GMT+01:00 Sebastien <se...@gmail.com>:
>
> > Hi, see #bind()
> >
>

Re: behavior passing components during construction

Posted by Ronny Pscheidl <jo...@googlemail.com>.
this is not the use case. collapsebehavior is set to component icon which
toggles the target component. the initial state of target has to be
collapsed. my approach is that the collapsebehavior could also set the
needed css class to the target component and not to add this manually to
the target component.

public class CollapseBehavior extends Behavior {
   private final IModel<String> targetMarkupId;

   public CollapseBehavior(Component target) {
      target.setOutputMarkupId(true);
      target.add(new CssClassAppender("collapse"));
      targetMarkupId = Model.of(target.getOutputMarkupId());
   }

   public void onBind(Component component) {
      component.add(new AttributeAppender("data-target", "#" +
targetMarkupId));
      component.add(new AttributeAppender("collapse", "toggle"));
   }
   ....
}

is there a way to let a behavior interact with two components?


2015-12-04 21:57 GMT+01:00 Sebastien <se...@gmail.com>:

> Hi, see #bind()
>

Re: behavior passing components during construction

Posted by Sebastien <se...@gmail.com>.
Hi, see #bind()