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


>
> 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
>
>

Reply via email to