You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Kaspar Fischer <fi...@inf.ethz.ch> on 2008/08/18 16:01:01 UTC

Should AjaxEventBehaviour's not be removed?

I need a certain div to appear when the user mouse-overs an image.
For this, i added a behaviour to the image:

     add(imageDiv.add(new AjaxEventBehavior("onmouseover")
     {
       @Override
       protected void onEvent(AjaxRequestTarget target)
       {
         div.setVisible(true);
         imageDiv.remove(this);
         target.addComponent(div);
         target.addComponent(imageDiv);
       }
     }));

So I remove the behaviour once the div has been shown.

If I mouse-over the image twice in a row very fast, I get a

   java.lang.IllegalStateException: No behavior listener found with  
behaviorId 0

I suppose that Wicket postpones the second request and when it is  
exectured
later on, the behaviour has already been removed.

What is the best way to have a JS behaviour executed only once?

Thanks in advance,
Kaspar

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


Re: Should AjaxEventBehaviour's not be removed?

Posted by Kaspar Fischer <fi...@inf.ethz.ch>.
Thanks a lot, Timo, for the advice. With your suggestion I got it
working without problems.

Kaspar

--

     add(imageDiv.add(new AjaxEventBehavior("onmouseover")
     {
       private boolean called;

       @Override
       protected void onEvent(AjaxRequestTarget target)
       {
         // Show
         div.setVisible(true);

         // Only do it once (to save bandwith)
         called = true;

         // Update affected components
         target.addComponent(div);
         target.addComponent(imageDiv);
       }

       @Override
       public boolean isEnabled(Component component)
       {
         return !called;
       }
     }));

On 19.08.2008, at 04:37, Timo Rantalaiho wrote:

> On Mon, 18 Aug 2008, Kaspar Fischer wrote:
>> I suppose that Wicket postpones the second request and when it is
>> exectured
>> later on, the behaviour has already been removed.
>
> This sounds likely, as the requests are processed
> serially.
>
>> What is the best way to have a JS behaviour executed only once?
>
> It is not so common to remove behaviours, but to toggle
> their "enabled" property. So maybe you can do something
> like this
>
>    add(imageDiv.add(new AjaxEventBehavior("onmouseover")
>    {
>      private boolean called;
>      @Override
>      protected void onEvent(AjaxRequestTarget target)
>      {
>        div.setVisible(true);
>        imageDiv.remove(this);
>        target.addComponent(div);
>        target.addComponent(imageDiv);
>        called = true;
>      }
>      @Override
>      protected boolean isEnabled()
>      {
>          return !called;
>      }
>    }));
>
> or if it doesn't do the trick, just do the check in onEvent().
>
> Best wishes,
> Timo
>
> -- 
> Timo Rantalaiho
> Reaktor Innovations Oy    <URL: http://www.ri.fi/ >
>
> ---------------------------------------------------------------------
> 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: Should AjaxEventBehaviour's not be removed?

Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Mon, 18 Aug 2008, Kaspar Fischer wrote:
> I suppose that Wicket postpones the second request and when it is  
> exectured
> later on, the behaviour has already been removed.

This sounds likely, as the requests are processed 
serially.

> What is the best way to have a JS behaviour executed only once?

It is not so common to remove behaviours, but to toggle 
their "enabled" property. So maybe you can do something 
like this

    add(imageDiv.add(new AjaxEventBehavior("onmouseover")                       
    {
      private boolean called;
      @Override                                                                 
      protected void onEvent(AjaxRequestTarget target)                          
      {                                                                         
        div.setVisible(true);                                                   
        imageDiv.remove(this);                                                  
        target.addComponent(div);                                               
        target.addComponent(imageDiv);
        called = true;
      }
      @Override 
      protected boolean isEnabled() 
      {
          return !called;
      }                                                             
    }));                                                                        

or if it doesn't do the trick, just do the check in onEvent().

Best wishes,
Timo

-- 
Timo Rantalaiho           
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

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