You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Carlson Weber <cw...@mastercim.com.br> on 2010/10/13 15:13:01 UTC

Problem with IHeaderResponse.renderOnEventJavascript

I searched all over the internet and it seems that no one uses this
method. The thing is that I am almost thinking that this is a bug,
because it's really not logic to me. What I am trying to do: I am
creating a panel that I will use in a lot of forms, and I want to
attach some javascript code to my onKeyDown HTML event of this
TextField, so my code looks like this:


container.getHeaderResponse().renderOnEventJavascript("document.getElementById('"+txtCliente.getMarkupId()+"')",
"onKeyDown", "if (window.event.altKey){if (window.event.keyCode ==
80){document.getElementById('" + link.getMarkupId() +
"').onclick();}}");

It's basically this: When user press Ctrl+P while TextField focused,
it will click on a link, that is another component of my panel. When
it renders the Javascript there's a problem: It render on the HEAD
section of HTML, referencing the TextField with a variable (with a
random name), but BEFORE the TextField was rendered on the HTML. The
result of this is that Firefox and Chrome gives an error , saying that
the component doesn't exist, and, in fact it doesn't, because the DOM
still didn't load! So, probably I am doing this wrong, does anyone can
help me with that? And what's the reason of this function
renderOnEventJavascript if it renders on the head? It will always give
an error message saying the component is null, am I right? I am using
the latest release of 1.4 series. Thank you very much guys!

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


Re: Problem with IHeaderResponse.renderOnEventJavascript

Posted by Carlson Weber <cw...@mastercim.com.br>.
Ok guys, it worked , I used the AttributeModifier. The thing is, why
there's is that renderOnEventJavascript? This should be a method
attached maybe to the domready event... Do you think that is worth
submiting a patch? Or this is the correct behavior? (IMHO this isn't
all right). Anyway, problem solved, thank you all!

2010/10/13 Michael O'Cleirigh <mi...@rivulet.ca>:
>  Hello,
>
> You can use:
>
> textfield.add (new AttributeModifier ("onkeydown", true, "if
> (window.event.altKey){if (window.event.keyCode ==
> 80){document.getElementById('" + link.getMarkupId() +
> "').onclick();}}");
>
> If you do this within the Component.onInitialize() you will have the true
> markupid of the link available.
>
> If you wanted to get rid of the link altogether you could just attach an
> ajax event directly to the text field like:
>
> textfield.add (new AjaxEventBehavior ("onkeydown") {
>
>    @Override
>    protected void onEvent(final AjaxRequestTarget target) {
>            // do your serverside logic
>    }
>
>    @Override
>   protected CharSequence getPreconditionScript() {
>        // if this evaluates to true in the browser then the event will be
> executed.
>        return "return window.event.altKey && window.event.keyCode == 80;";
>    }
>
> }):
>
> This is untested but I think it should work,
>
> Regards,
>
> Mike
>
>> I searched all over the internet and it seems that no one uses this
>> method. The thing is that I am almost thinking that this is a bug,
>> because it's really not logic to me. What I am trying to do: I am
>> creating a panel that I will use in a lot of forms, and I want to
>> attach some javascript code to my onKeyDown HTML event of this
>> TextField, so my code looks like this:
>>
>>
>>
>> container.getHeaderResponse().renderOnEventJavascript("document.getElementById('"+txtCliente.getMarkupId()+"')",
>> "onKeyDown", "if (window.event.altKey){if (window.event.keyCode ==
>> 80){document.getElementById('" + link.getMarkupId() +
>> "').onclick();}}");
>>
>> It's basically this: When user press Ctrl+P while TextField focused,
>> it will click on a link, that is another component of my panel. When
>> it renders the Javascript there's a problem: It render on the HEAD
>> section of HTML, referencing the TextField with a variable (with a
>> random name), but BEFORE the TextField was rendered on the HTML. The
>> result of this is that Firefox and Chrome gives an error , saying that
>> the component doesn't exist, and, in fact it doesn't, because the DOM
>> still didn't load! So, probably I am doing this wrong, does anyone can
>> help me with that? And what's the reason of this function
>> renderOnEventJavascript if it renders on the head? It will always give
>> an error message saying the component is null, am I right? I am using
>> the latest release of 1.4 series. Thank you very much guys!
>>
>> ---------------------------------------------------------------------
>> 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
>
>

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


Re: Problem with IHeaderResponse.renderOnEventJavascript

Posted by Michael O'Cleirigh <mi...@rivulet.ca>.
  Hello,

You can use:

textfield.add (new AttributeModifier ("onkeydown", true, "if 
(window.event.altKey){if (window.event.keyCode ==
80){document.getElementById('" + link.getMarkupId() +
"').onclick();}}");

If you do this within the Component.onInitialize() you will have the 
true markupid of the link available.

If you wanted to get rid of the link altogether you could just attach an 
ajax event directly to the text field like:

textfield.add (new AjaxEventBehavior ("onkeydown") {

     @Override
     protected void onEvent(final AjaxRequestTarget target) {
             // do your serverside logic
     }

     @Override
    protected CharSequence getPreconditionScript() {
         // if this evaluates to true in the browser then the event will 
be executed.
         return "return window.event.altKey && window.event.keyCode == 80;";
     }

}):

This is untested but I think it should work,

Regards,

Mike

> I searched all over the internet and it seems that no one uses this
> method. The thing is that I am almost thinking that this is a bug,
> because it's really not logic to me. What I am trying to do: I am
> creating a panel that I will use in a lot of forms, and I want to
> attach some javascript code to my onKeyDown HTML event of this
> TextField, so my code looks like this:
>
>
> container.getHeaderResponse().renderOnEventJavascript("document.getElementById('"+txtCliente.getMarkupId()+"')",
> "onKeyDown", "if (window.event.altKey){if (window.event.keyCode ==
> 80){document.getElementById('" + link.getMarkupId() +
> "').onclick();}}");
>
> It's basically this: When user press Ctrl+P while TextField focused,
> it will click on a link, that is another component of my panel. When
> it renders the Javascript there's a problem: It render on the HEAD
> section of HTML, referencing the TextField with a variable (with a
> random name), but BEFORE the TextField was rendered on the HTML. The
> result of this is that Firefox and Chrome gives an error , saying that
> the component doesn't exist, and, in fact it doesn't, because the DOM
> still didn't load! So, probably I am doing this wrong, does anyone can
> help me with that? And what's the reason of this function
> renderOnEventJavascript if it renders on the head? It will always give
> an error message saying the component is null, am I right? I am using
> the latest release of 1.4 series. Thank you very much guys!
>
> ---------------------------------------------------------------------
> 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: Problem with IHeaderResponse.renderOnEventJavascript

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

Indeed it looks this method works only when the target element is already
available.
It is used by
org.apache.wicket.markup.html.internal.HeaderResponse.renderOnDomReadyJavascript(String)
and
org.apache.wicket.markup.html.internal.HeaderResponse.renderOnLoadJavascript(String)
but both of them use 'window' as target and it is always available.

To solve your problem you can use renderOnDomReadyJavascript(String) with
parameter: "Wicket.Event.add(Wicket.$("+theId+", 'keydown', function(event)
{...}))

On Wed, Oct 13, 2010 at 3:13 PM, Carlson Weber <cw...@mastercim.com.br>wrote:

> I searched all over the internet and it seems that no one uses this
> method. The thing is that I am almost thinking that this is a bug,
> because it's really not logic to me. What I am trying to do: I am
> creating a panel that I will use in a lot of forms, and I want to
> attach some javascript code to my onKeyDown HTML event of this
> TextField, so my code looks like this:
>
>
>
> container.getHeaderResponse().renderOnEventJavascript("document.getElementById('"+txtCliente.getMarkupId()+"')",
> "onKeyDown", "if (window.event.altKey){if (window.event.keyCode ==
> 80){document.getElementById('" + link.getMarkupId() +
> "').onclick();}}");
>
> It's basically this: When user press Ctrl+P while TextField focused,
> it will click on a link, that is another component of my panel. When
> it renders the Javascript there's a problem: It render on the HEAD
> section of HTML, referencing the TextField with a variable (with a
> random name), but BEFORE the TextField was rendered on the HTML. The
> result of this is that Firefox and Chrome gives an error , saying that
> the component doesn't exist, and, in fact it doesn't, because the DOM
> still didn't load! So, probably I am doing this wrong, does anyone can
> help me with that? And what's the reason of this function
> renderOnEventJavascript if it renders on the head? It will always give
> an error message saying the component is null, am I right? I am using
> the latest release of 1.4 series. Thank you very much guys!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>