You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by gump lee <ze...@gmail.com> on 2015/07/09 04:11:29 UTC

How to stop event propagation?

Hello All,

I am using Wicket 6.19. I added a AjaxFormComponentUpdatingBehavior("keypress")
to a textfield so that when press any key will trigger my application to
update something. I want this only happen when user press 'Enter' key. so
that I override the method updateAjaxAttributes in the
AjaxFormComponentUpdatingBehavior

@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
AjaxCallListener myAjaxCallListener = new AjaxCallListener() {
@Override
public CharSequence getPrecondition(Component component) {
String script = "var keycode = Wicket.Event.keyCode(attrs.event);"
+ "if (keycode == 13) {"
+ "    return true;"
+ "} else {" + "    return false;}";

return script;
}
};
attributes.getAjaxCallListeners().add(myAjaxCallListener);
attributes.setEventPropagation(EventPropagation.STOP);
}

However, after the ajax call, the form submit also triggered. Does anyone
know the correct way to stop this form submit?


Thanks and best regards,
Gump

Re: How to stop event propagation?

Posted by Martin Grigorov <mg...@apache.org>.
Oh, right!
Sorry! It was too early for me it seems.

The this should work.
You can simplify it a bit:

AjaxCallListener myAjaxCallListener = new AjaxCallListener();
myAjaxCallListener.onPrecondition("return Wicket.Event.keyCode(attrs.event)
=== 13");

No need to set the event propagation because STOP is the default:
https://github.com/apache/wicket/blob/wicket-6.x/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/AjaxRequestAttributes.java#L85
But keep it there because this is changed in Wicket 7.x
...

Put a breakpoint at
https://github.com/apache/wicket/blob/wicket-6.x/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js#L1916
and see what happens

Martin Grigorov
Freelancer. Available for hire!
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Thu, Jul 9, 2015 at 11:05 AM, gump lee <ze...@gmail.com> wrote:

> I use the following code to register the AjaxCallListener
>
> @Override
> protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
>
> AjaxCallListener myAjaxCallListener = new AjaxCallListener() {
> @Override
> public CharSequence getPrecondition(Component component) {
> String script = "var keycode = Wicket.Event.keyCode(attrs.event);"
> + "if (keycode == 13) {"
> + "    return true;"
> + "} else {" + "    return false;}";
>
> return script;
> }
> };
> * attributes.getAjaxCallListeners().add(myAjaxCallListener);*
> attributes.setEventPropagation(EventPropagation.STOP);
> super.updateAjaxAttributes(attributes);
> }
>
> or would you show me the correct way to register the AjaxCallListener?
>
> On Thu, Jul 9, 2015 at 2:45 PM, Martin Grigorov <mg...@apache.org>
> wrote:
>
> > There is no attachment in both of your mails.
> > I don't see this line in your first message.
> >
> > Martin Grigorov
> > Freelancer. Available for hire!
> > Wicket Training and Consulting
> > https://twitter.com/mtgrigorov
> >
> > On Thu, Jul 9, 2015 at 9:42 AM, gump lee <ze...@gmail.com> wrote:
> >
> > > Thanks,
> > >
> > > Actually I did it. refer to my attached code, I register my
> > > AjaxcallListener. Did I register the AjaxCallListener correctly?
> > >
> > > On Thu, Jul 9, 2015 at 2:23 PM, Martin Grigorov <mg...@apache.org>
> > > wrote:
> > >
> > > > Hi,
> > > >
> > > > You need to register the AjaxCallListener:
> > > > attributes.getAjaxCallListeners().add(myAjaxCallListener)
> > > >
> > > > Martin Grigorov
> > > > Freelancer. Available for hire!
> > > > Wicket Training and Consulting
> > > > https://twitter.com/mtgrigorov
> > > >
> > > > On Thu, Jul 9, 2015 at 5:11 AM, gump lee <ze...@gmail.com> wrote:
> > > >
> > > > > Hello All,
> > > > >
> > > > > I am using Wicket 6.19. I added a
> > > > > AjaxFormComponentUpdatingBehavior("keypress")
> > > > > to a textfield so that when press any key will trigger my
> application
> > > to
> > > > > update something. I want this only happen when user press 'Enter'
> > key.
> > > so
> > > > > that I override the method updateAjaxAttributes in the
> > > > > AjaxFormComponentUpdatingBehavior
> > > > >
> > > > > @Override
> > > > > protected void updateAjaxAttributes(AjaxRequestAttributes
> > attributes) {
> > > > > super.updateAjaxAttributes(attributes);
> > > > > AjaxCallListener myAjaxCallListener = new AjaxCallListener() {
> > > > > @Override
> > > > > public CharSequence getPrecondition(Component component) {
> > > > > String script = "var keycode = Wicket.Event.keyCode(attrs.event);"
> > > > > + "if (keycode == 13) {"
> > > > > + "    return true;"
> > > > > + "} else {" + "    return false;}";
> > > > >
> > > > > return script;
> > > > > }
> > > > > };
> > > > > attributes.getAjaxCallListeners().add(myAjaxCallListener);
> > > > > attributes.setEventPropagation(EventPropagation.STOP);
> > > > > }
> > > > >
> > > > > However, after the ajax call, the form submit also triggered. Does
> > > anyone
> > > > > know the correct way to stop this form submit?
> > > > >
> > > > >
> > > > > Thanks and best regards,
> > > > > Gump
> > > > >
> > > >
> > >
> >
>

Re: How to stop event propagation?

Posted by gump lee <ze...@gmail.com>.
I use the following code to register the AjaxCallListener

@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {

AjaxCallListener myAjaxCallListener = new AjaxCallListener() {
@Override
public CharSequence getPrecondition(Component component) {
String script = "var keycode = Wicket.Event.keyCode(attrs.event);"
+ "if (keycode == 13) {"
+ "    return true;"
+ "} else {" + "    return false;}";

return script;
}
};
* attributes.getAjaxCallListeners().add(myAjaxCallListener);*
attributes.setEventPropagation(EventPropagation.STOP);
super.updateAjaxAttributes(attributes);
}

or would you show me the correct way to register the AjaxCallListener?

On Thu, Jul 9, 2015 at 2:45 PM, Martin Grigorov <mg...@apache.org>
wrote:

> There is no attachment in both of your mails.
> I don't see this line in your first message.
>
> Martin Grigorov
> Freelancer. Available for hire!
> Wicket Training and Consulting
> https://twitter.com/mtgrigorov
>
> On Thu, Jul 9, 2015 at 9:42 AM, gump lee <ze...@gmail.com> wrote:
>
> > Thanks,
> >
> > Actually I did it. refer to my attached code, I register my
> > AjaxcallListener. Did I register the AjaxCallListener correctly?
> >
> > On Thu, Jul 9, 2015 at 2:23 PM, Martin Grigorov <mg...@apache.org>
> > wrote:
> >
> > > Hi,
> > >
> > > You need to register the AjaxCallListener:
> > > attributes.getAjaxCallListeners().add(myAjaxCallListener)
> > >
> > > Martin Grigorov
> > > Freelancer. Available for hire!
> > > Wicket Training and Consulting
> > > https://twitter.com/mtgrigorov
> > >
> > > On Thu, Jul 9, 2015 at 5:11 AM, gump lee <ze...@gmail.com> wrote:
> > >
> > > > Hello All,
> > > >
> > > > I am using Wicket 6.19. I added a
> > > > AjaxFormComponentUpdatingBehavior("keypress")
> > > > to a textfield so that when press any key will trigger my application
> > to
> > > > update something. I want this only happen when user press 'Enter'
> key.
> > so
> > > > that I override the method updateAjaxAttributes in the
> > > > AjaxFormComponentUpdatingBehavior
> > > >
> > > > @Override
> > > > protected void updateAjaxAttributes(AjaxRequestAttributes
> attributes) {
> > > > super.updateAjaxAttributes(attributes);
> > > > AjaxCallListener myAjaxCallListener = new AjaxCallListener() {
> > > > @Override
> > > > public CharSequence getPrecondition(Component component) {
> > > > String script = "var keycode = Wicket.Event.keyCode(attrs.event);"
> > > > + "if (keycode == 13) {"
> > > > + "    return true;"
> > > > + "} else {" + "    return false;}";
> > > >
> > > > return script;
> > > > }
> > > > };
> > > > attributes.getAjaxCallListeners().add(myAjaxCallListener);
> > > > attributes.setEventPropagation(EventPropagation.STOP);
> > > > }
> > > >
> > > > However, after the ajax call, the form submit also triggered. Does
> > anyone
> > > > know the correct way to stop this form submit?
> > > >
> > > >
> > > > Thanks and best regards,
> > > > Gump
> > > >
> > >
> >
>

Re: How to stop event propagation?

Posted by Martin Grigorov <mg...@apache.org>.
There is no attachment in both of your mails.
I don't see this line in your first message.

Martin Grigorov
Freelancer. Available for hire!
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Thu, Jul 9, 2015 at 9:42 AM, gump lee <ze...@gmail.com> wrote:

> Thanks,
>
> Actually I did it. refer to my attached code, I register my
> AjaxcallListener. Did I register the AjaxCallListener correctly?
>
> On Thu, Jul 9, 2015 at 2:23 PM, Martin Grigorov <mg...@apache.org>
> wrote:
>
> > Hi,
> >
> > You need to register the AjaxCallListener:
> > attributes.getAjaxCallListeners().add(myAjaxCallListener)
> >
> > Martin Grigorov
> > Freelancer. Available for hire!
> > Wicket Training and Consulting
> > https://twitter.com/mtgrigorov
> >
> > On Thu, Jul 9, 2015 at 5:11 AM, gump lee <ze...@gmail.com> wrote:
> >
> > > Hello All,
> > >
> > > I am using Wicket 6.19. I added a
> > > AjaxFormComponentUpdatingBehavior("keypress")
> > > to a textfield so that when press any key will trigger my application
> to
> > > update something. I want this only happen when user press 'Enter' key.
> so
> > > that I override the method updateAjaxAttributes in the
> > > AjaxFormComponentUpdatingBehavior
> > >
> > > @Override
> > > protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
> > > super.updateAjaxAttributes(attributes);
> > > AjaxCallListener myAjaxCallListener = new AjaxCallListener() {
> > > @Override
> > > public CharSequence getPrecondition(Component component) {
> > > String script = "var keycode = Wicket.Event.keyCode(attrs.event);"
> > > + "if (keycode == 13) {"
> > > + "    return true;"
> > > + "} else {" + "    return false;}";
> > >
> > > return script;
> > > }
> > > };
> > > attributes.getAjaxCallListeners().add(myAjaxCallListener);
> > > attributes.setEventPropagation(EventPropagation.STOP);
> > > }
> > >
> > > However, after the ajax call, the form submit also triggered. Does
> anyone
> > > know the correct way to stop this form submit?
> > >
> > >
> > > Thanks and best regards,
> > > Gump
> > >
> >
>

Re: How to stop event propagation?

Posted by gump lee <ze...@gmail.com>.
Thanks,

Actually I did it. refer to my attached code, I register my
AjaxcallListener. Did I register the AjaxCallListener correctly?

On Thu, Jul 9, 2015 at 2:23 PM, Martin Grigorov <mg...@apache.org>
wrote:

> Hi,
>
> You need to register the AjaxCallListener:
> attributes.getAjaxCallListeners().add(myAjaxCallListener)
>
> Martin Grigorov
> Freelancer. Available for hire!
> Wicket Training and Consulting
> https://twitter.com/mtgrigorov
>
> On Thu, Jul 9, 2015 at 5:11 AM, gump lee <ze...@gmail.com> wrote:
>
> > Hello All,
> >
> > I am using Wicket 6.19. I added a
> > AjaxFormComponentUpdatingBehavior("keypress")
> > to a textfield so that when press any key will trigger my application to
> > update something. I want this only happen when user press 'Enter' key. so
> > that I override the method updateAjaxAttributes in the
> > AjaxFormComponentUpdatingBehavior
> >
> > @Override
> > protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
> > super.updateAjaxAttributes(attributes);
> > AjaxCallListener myAjaxCallListener = new AjaxCallListener() {
> > @Override
> > public CharSequence getPrecondition(Component component) {
> > String script = "var keycode = Wicket.Event.keyCode(attrs.event);"
> > + "if (keycode == 13) {"
> > + "    return true;"
> > + "} else {" + "    return false;}";
> >
> > return script;
> > }
> > };
> > attributes.getAjaxCallListeners().add(myAjaxCallListener);
> > attributes.setEventPropagation(EventPropagation.STOP);
> > }
> >
> > However, after the ajax call, the form submit also triggered. Does anyone
> > know the correct way to stop this form submit?
> >
> >
> > Thanks and best regards,
> > Gump
> >
>

Re: How to stop event propagation?

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

You need to register the AjaxCallListener:
attributes.getAjaxCallListeners().add(myAjaxCallListener)

Martin Grigorov
Freelancer. Available for hire!
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Thu, Jul 9, 2015 at 5:11 AM, gump lee <ze...@gmail.com> wrote:

> Hello All,
>
> I am using Wicket 6.19. I added a
> AjaxFormComponentUpdatingBehavior("keypress")
> to a textfield so that when press any key will trigger my application to
> update something. I want this only happen when user press 'Enter' key. so
> that I override the method updateAjaxAttributes in the
> AjaxFormComponentUpdatingBehavior
>
> @Override
> protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
> super.updateAjaxAttributes(attributes);
> AjaxCallListener myAjaxCallListener = new AjaxCallListener() {
> @Override
> public CharSequence getPrecondition(Component component) {
> String script = "var keycode = Wicket.Event.keyCode(attrs.event);"
> + "if (keycode == 13) {"
> + "    return true;"
> + "} else {" + "    return false;}";
>
> return script;
> }
> };
> attributes.getAjaxCallListeners().add(myAjaxCallListener);
> attributes.setEventPropagation(EventPropagation.STOP);
> }
>
> However, after the ajax call, the form submit also triggered. Does anyone
> know the correct way to stop this form submit?
>
>
> Thanks and best regards,
> Gump
>