On 1 mar, 14:02, Andrey <razumovsky.and...@gmail.com> wrote:
> Hi list,
>
> I want to setup a listener to a widget from java code using JSNI. The
> listener will call java method. Here's my code:
>
> class PageToolbar extends PagingToolbar {
> private native void patch() /*-{
>         var pagingToolbar =
> th...@com.gwtext.client.widgets.component::getOrCreateJsObj()();
>
>         pagingToolbar.field.on("keydown", function(e) {
>                 alert("1");
>                 th...@mypackage.pagetoolbar::mymethod()();
>                 alert("2");
>         }, pagingToolbar);}-*/;
>
> private void mymethod() {
>   ...
>
> }
> }
>
> The problem is that this code does not work, because "this" is not
> PageToolbar inside event handling function. Likely this is because
> this is inner function. Is there any way I can access enclosing "this"
> instance (that would be PageToolbar)?

Not sure it'll work but, how about:
   var mymethod = t...@mypackage.pagetoolbar::mymethod();
   pagingToolbar.field.on("keydown", function(e) {
                alert("1");
                th...@mypackage.pagetoolbar::mymethod()();
                alert("2");
        }, pagingToolbar);

or a variation:
   var mymethod = function() {
      t...@mypackage.pagetoolbar::mymethod()();
   };

or eventually, closer to what JS frameworks do:
   var that = this;
   pagingToolbar.field.on("keydown", function(e) {
                alert("1");
                t...@mypackage.pagetoolbar::mymethod()();
                alert("2");
        }, pagingToolbar);
   };

I think the "this" inside the function is the "pagingToolbar" JS
variable.
I don't know the JS code you're using but maybe this would work:
   pagingToolbar.field.on("keydown", function(e) {
                alert("1");
                t...@mypackage.pagetoolbar::mymethod()();
                alert("2");
        }, this);

(note the "this" passed as the second argument to on(), instead of
pagingToolbar; in many frameworks this seems to be the scope (i.e.
"this") for the callback)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to