This alas does not work:( First I am using wicket 7.2.0 and the getModelObject does not exist so I finally made:
@Override protected void onComponentTag(ComponentTag tag) { add(new AttributeAppender("class", " number")); if(getDefaultModel().getObject() instanceof Number){ Number number = (Number) getDefaultModelObject(); if(number.doubleValue()<0.0) add(new AttributeAppender("class", " negative")); } } This however gives the following exception: org.apache.wicket.WicketRuntimeException: Cannot modify component hierarchy after render phase has started (page version cant change then anymore) at org.apache.wicket.Component.checkHierarchyChange(Component.java:3623) at org.apache.wicket.Component.addStateChange(Component.java:3538) at org.apache.wicket.Behaviors.add(Behaviors.java:55) at org.apache.wicket.Component.add(Component.java:4559) at nl.codetribe.budget.web.common.MoneyLabel.onComponentTag(MoneyLabel.java:36) at org.apache.wicket.Component.internalRenderComponent(Component.java:2521) On Fri, Apr 1, 2016 at 3:11 PM Tobias Soloschenko tobiassolosche...@googlemail.com <http://mailto:tobiassolosche...@googlemail.com> wrote: Cool - thanks! > > kind regards > > Tobias > > > Am 01.04.2016 um 14:56 schrieb Martin Grigorov <mgrigo...@apache.org>: > > > > I've added an example with > > > https://github.com/apache/wicket/commit/2417322673238d57a794c237e84dd5dacf890804 > > > > I think we should explain that those are just convenience. It is better > to > > use the "old way" one cares about memory usage. I'll add it soon. > > > > Martin Grigorov > > Wicket Training and Consulting > > https://twitter.com/mtgrigorov > > > > On Fri, Apr 1, 2016 at 2:48 PM, Tobias Soloschenko < > > tobiassolosche...@googlemail.com> wrote: > > > >> Hi, > >> > >> would be great to see some information how to use lambda models in the > >> user guide. > >> > >> kind regards > >> > >> Tobias > >> > >>>> Am 01.04.2016 um 13:57 schrieb Martin Grigorov <mgrigo...@apache.org > >: > >>>> > >>>> On Fri, Apr 1, 2016 at 12:45 AM, Sven Meier <s...@meiers.net> wrote: > >>>> > >>>> Hi, > >>>> > >>>> yeah, of course this is right: > >>>> > >>>> protected void onComponentTag(final ComponentTag tag) { > >>>> > >>>> if (getModelObject().doubleValue() < 0.0) { > >>>> tag.put("class", " negative"); > >>>> } > >>>> } > >>>> > >>>> Otherwise you'll end up with one additional behavior for each render - > >>>> stupid copy-paste error :/. > >>>> > >>>> BTW this might be an additional candidate for the new Wicket 8 > lambdas: > >>>> > >>>> label.add(onTag(tag -> if (getModelObject().doubleValue() < 0.0) > >>>> tag.put("class", "negative") )); > >>> > >>> +1 > >>> > >> > >> +1 > >> > >>> > >>>> > >>>> Sven > >>>> > >>>> > >>>> > >>>>> On 31.03.2016 21:16, Martin Grigorov wrote: > >>>>> > >>>>> Hi, > >>>>> > >>>>> On Thu, Mar 31, 2016 at 4:29 PM, Sven Meier <s...@meiers.net> wrote: > >>>>> > >>>>> Hi, > >>>>>> > >>>>>> you can access a component's model with #getDefaultModel() or - if > it > >> is > >>>>>> a > >>>>>> generic component - with #getModel(). > >>>>>> > >>>>>> To properly 'bind' to a model you should delay invocation of > >> #getObject() > >>>>>> as late as possible, so better overwrite #onComponentTag(): > >>>>>> > >>>>>> protected void onComponentTag(final ComponentTag tag) { > >>>>>> > >>>>>> if (getModelObject().doubleValue() < 0.0) { > >>>>>> add(new AttributeAppender("class", " negative")); > >>>>>> > >>>>>> Since you are in #onComponentTag() there is no reason to add a > >> Behavior. > >>>>> Just use the tag: tag.append("class", "negative", " "); > >>>>> > >>>>> > >>>>> } > >>>>>> } > >>>>>> > >>>>>> > >>>>>> Have fun > >>>>>> Sven > >>>>>> > >>>>>> > >>>>>> > >>>>>> > >>>>>> On 31.03.2016 16:06, Ron Smits wrote: > >>>>>> > >>>>>> I have searched but I dont find a clear way of getting the model > when > >> one > >>>>>>> is creating a custom component: > >>>>>>> > >>>>>>> > >>>>>>> public MoneyLabel(String id) { > >>>>>>> super(id); > >>>>>>> add(new AttributeAppender("class", " number")); > >>>>>>> } > >>>>>>> > >>>>>>> It sounds like you always need the model. > >>>>> If there is a CompoundPropertyModel in the parents' hierarchy then > move > >>>>> your logic to #onComponentTag(), as per Sven's suggestion. > >>>>> If there is no CPM in the parents then just remove this constructor. > >>>>> > >>>>> > >>>>> public MoneyLabel(String id, Model<? extends BigDecimal> > >> bigDecimalModel) > >>>>>>> { > >>>>>>> super(id, bigDecimalModel); > >>>>>>> add(new AttributeAppender("class", " number")); > >>>>>>> if (bigDecimalModel.getObject().doubleValue() < 0.0) { > >>>>>>> add(new AttributeAppender("class", " negative")); > >>>>>>> } > >>>>>>> } > >>>>>>> > >>>>>>> In the second constructor I can use the supplied model to determine > >> to > >>>>>>> add > >>>>>>> the negative class. However in the first constructor I cannot find > a > >> way > >>>>>>> to > >>>>>>> access the model. > >>>>>>> > >>>>>>> What am I missing? > >>>>>>> > >>>>>>> Ron > >>>>>>> > >>>>>>> > >>>>>>> > >>>>>>> > --------------------------------------------------------------------- > >>>>>> 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 > >> > >> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org > For additional commands, e-mail: users-h...@wicket.apache.org > >