Hi, thanks for the answers again!

Ok, I have some working code now! I think my issues were a combined
not understand "this" became the $wnd object when written how I had
it, plus not understanding what $entry actually does. Having said that
the docs on $entry could be better than the few sentences given.

Incidentally, I had some similar code where I had tried wrapping in
"function() { }" but hadn't gotten that working.

Olivier - your solutions worked first time for me.

Thomas - your solution worked from 6 worked but at first I didn't
include both pairs of brackets after the function call "MyMethod" but
it turns out they are required. With only one set (i.e. no arguments),
it just doesn't work. I thought that was legal but maybe not.

Now I just have to decide which method is best. I think I prefer
passing in the instance as a parameter of the inject method - I just
prefer the argument to be strongly typed on the Java side (being
primarily a Java developer). Olivier's code to work against memory
leaks also seems sensible to me. I have a static method which does
both of these now and works fine and think I understand the area a lot
better thanks to you two. It does seem like a lot to write just to get
a callback though! Maybe some decent examples like this would be
useful in the GWT documentation pages?



On Mar 23, 8:30 am, Olivier Monaco <olivier.mon...@free.fr> wrote:
> Hi Thomas,
>
> The problem with memory leaks in browser is that it's sometimes hard
> to understand where they come from. Here, we are writting a little
> piece of code that will be transformed (replacement of @...()) and
> incorporated into a more complexe code. I don't want to look at the
> generated code to check if leaks are possibile. So, I always use the
> syntax I give so that I'm sure there will not be any leak... It's some
> type of discipline.
>
> Using a static method to inject the code seems to me being the same
> thing...
>
> Olivier
>
> On 23 mar, 00:07, Thomas Broyer <t.bro...@gmail.com> wrote:
>
> > On 22 mar, 23:42, Olivier Monaco <olivier.mon...@free.fr> wrote:
>
> > > The "call" method of a JavaScript function allows you to call it with
> > > a differente "this". Then, using "$wnd.js_callback.call(otherObject)",
> > > the "this" variable reference otherObject. So the glue is:
>
> > > var that = this;
> > > $wnd.js_callback = function() {
> > >     $entry(@mypackage.MyClass::MyMethod()).call(that);
>
> > > }
>
> > > When "js_callback" will be called, MyMethod is called on "that", the
> > > JavaScript object representing the MyClass instance. Finally, to avoid
> > > memory leaks of closure:
>
> > > $wnd.js_callback = (function(obj) {
> > >     return function() {
> > >         $entry(@mypackage.MyClass::MyMethod()).call(obj);
> > >     };
>
> > > })(this);
>
> > ... or more simply:
> > var that = this;
> > $wnd.js_callback = $entry(function() { that.
> > @mypackage.MyClass::MyMethod()(); });
>
> > (this is what I should have written previously, sorry for the buggy
> > code)
>
> > As for the leak, are you sure there really is one once the code is
> > translated into JS? would it be better if the "inject" method were a
> > static one with the instance being passed as an argument (no use of
> > the "this"keyword within JSNI)?
>
>

-- 
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-tool...@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