one usecase is when you want the user to be able to change which
components will be created.
this is bad:
class mycomponent extends panel {
public mycomponent(string id) { add(newCounter("counter")); }
protected Component newCounter(String id) { return new Label(id, ....); }
}
because you are calling an overridable method from constructor, so
instead the call to newcounter can look like this
class mycomponent extends panel {
protected void onbeforerender() {
if (get("counter")==null) {
add(newCounter("counter");
}
super.onbeforerender();
}
protected Component newCounter(String id) { return new Label(id, ....); }
}
or with 1.5
class mycomponent extends panel {
protected void onInitialize() {
super.onInitialize();
add(newCounter("counter");
}
protected Component newCounter(String id) { return new Label(id, ....); }
}
-igor
On Fri, Apr 2, 2010 at 2:56 PM, mbrictson <[email protected]> wrote:
>
> Joseph,
>
> Could you elaborate on why adding components in onBeforeRender is "safe" and
> a "wicket good practice"? I haven't come across this very often in my
> projects. Under what circumstances would you recommend this approach?
>
>
> josephpachod wrote:
>>
>> hi
>>
>> The other day, I was busy creating reusable components. To make them
>> "safe", I used what I believe is a wicket good practices: adding the
>> components in onBeforeRender.
>>
>> In fact, it's not just in onBeforeRender, it's rather :
>> @Override
>> protected void onBeforeRender() {
>> if(!hasBeenRendered()){
>> // actual code
>> }
>> super.onBeforeRender();
>> }
>>
>> having done this stuff repeatedly, I felt a bit annoyed but these many "
>> if(!hasBeenRendered())" and the brackets/indentations it brings.
>> Furthermore, on the few occasions I really needed something done on each
>> onBeforeRender, it brought clutter to the code.
>> Last but not least, recently, I was helping a wicket beginner and
>> explaining this "onBeforeRender/if(!hasBeenRendered)" wasn't the best
>> moment I had.
>>
>> As such, I started to wonder if a simple "onInitialRender" method (or
>> similarly named) could be created ? It would run once and only once before
>> the first onBeforeRender.
>>
>> onBeforeRender would then return to what it should really mean (but still
>> have this handy hasBeenRendered() method just in case).
>>
>> In the end, it's trivial but would save a few keystrokes and bring some
>> clarity.
>>
>> What do you think of that ?
>>
>> thanks in advance
>> joseph
>>
>
> --
> View this message in context:
> http://old.nabble.com/What-about-an-onInitialRender-method---tp28122954p28123309.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]