You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Joseph Pachod <jo...@thomas-daily.de> on 2010/04/02 23:03:56 UTC

What about an onInitialRender method ?

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

Re: What about an onInitialRender method ?

Posted by mbrictson <ma...@55minutes.com>.
Good example. Thanks for clarifying.


igor.vaynberg wrote:
> 
> 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
> 

-- 
View this message in context: http://old.nabble.com/What-about-an-onInitialRender-method---tp28122954p28123955.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: What about an onInitialRender method ?

Posted by Igor Vaynberg <ig...@gmail.com>.
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 <ma...@55minutes.com> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: What about an onInitialRender method ?

Posted by mbrictson <ma...@55minutes.com>.
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: What about an onInitialRender method ?

Posted by Maarten Bosteels <mb...@gmail.com>.
Small fixes in the javadoc:

1) "Add this point in time "
 shouldn't that be "At this point in time" ?


2) "all parents must be have been added to their parents"
=> I would remove the "be"

Maarten

On Fri, Apr 2, 2010 at 11:09 PM, Igor Vaynberg <ig...@gmail.com>wrote:

> from Component.java in 1.5 (trunk)
>
>        /**
>         * Callback method invoked after the component was added to its
> parent AND you can walk up the
>         * hierarchy up until the Page. That is, all parents must be have
> been added to their parents as
>         * well. Add this point in time {@link #getMarkup() getMarkup} is
> guaranteed to be available.
>         * <p>
>         * This method is guaranteed to called only once
>         * </p>
>         * <p>
>         * If you don't like constructors to initialize your component, this
> is the method to use.
>         * </p>
>         */
>        protected void onInitialize()
>        {
>        }
>
> -igor
>
> On Fri, Apr 2, 2010 at 2:03 PM, Joseph Pachod
> <jo...@thomas-daily.de> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: What about an onInitialRender method ?

Posted by Igor Vaynberg <ig...@gmail.com>.
from Component.java in 1.5 (trunk)

	/**
	 * Callback method invoked after the component was added to its
parent AND you can walk up the
	 * hierarchy up until the Page. That is, all parents must be have
been added to their parents as
	 * well. Add this point in time {@link #getMarkup() getMarkup} is
guaranteed to be available.
	 * <p>
	 * This method is guaranteed to called only once
	 * </p>
	 * <p>
	 * If you don't like constructors to initialize your component, this
is the method to use.
	 * </p>
	 */
	protected void onInitialize()
	{
	}

-igor

On Fri, Apr 2, 2010 at 2:03 PM, Joseph Pachod
<jo...@thomas-daily.de> 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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org