about callAsync and scope

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

about callAsync and scope

oetiker
While qooxdoo is hurling towards 1.0 there is this question that is
bugging me.

I have learned, that using anonymous functions leads to grief when
debugging. So instead of

  server.callAsync(function(ret,exc,id){
     \\ handler code
  },arg1,arg2,arg3);

I am writing

  function handler(ret,exc,id){
     \\ handler code
  }

  server.callAsync(handler,arg1,arg2,arg3);

unfortunately handler runs out of scope. I can therefore not
access 'this' inside the handler. Unless I pass this into the
handler with the var that=this trick.

It works fine but it makes it impossible (afaik) to setup handlers
as private methods in the object.

In other methods like addListener it is possible to pass along the
scope the handler should be run in.

I would realy like to see a callAsyncScoped call that works similar
to addListener:

  callAsyncScoped(handler,scope,arg1,arg2,....)

cheers
tobi

--
Tobi Oetiker, OETIKER+PARTNER AG, Aarweg 15 CH-4600 Olten, Switzerland
http://it.oetiker.ch [hidden email] ++41 62 775 9902 / sb: -9900

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: about callAsync and scope

Noggin182
Hi Tobias,
You can your qx.lang.Function.bind

server.callAsync(qx.lang.Function.bind(handler, this), arg1, arg2...);

That should fix your issue :)

HTH
Matt

Tobias Oetiker wrote:

> While qooxdoo is hurling towards 1.0 there is this question that is
> bugging me.
>
> I have learned, that using anonymous functions leads to grief when
> debugging. So instead of
>
>   server.callAsync(function(ret,exc,id){
>      \\ handler code
>   },arg1,arg2,arg3);
>
> I am writing
>
>   function handler(ret,exc,id){
>      \\ handler code
>   }
>
>   server.callAsync(handler,arg1,arg2,arg3);
>
> unfortunately handler runs out of scope. I can therefore not
> access 'this' inside the handler. Unless I pass this into the
> handler with the var that=this trick.
>
> It works fine but it makes it impossible (afaik) to setup handlers
> as private methods in the object.
>
> In other methods like addListener it is possible to pass along the
> scope the handler should be run in.
>
> I would realy like to see a callAsyncScoped call that works similar
> to addListener:
>
>   callAsyncScoped(handler,scope,arg1,arg2,....)
>
> cheers
> tobi
>


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: about callAsync and scope

Fabian Jakobs
Administrator
In reply to this post by oetiker
Hi Tobias,

one solution to your problem is using qx.lang.Function.bind. With this
function you can create a wrapped version of your method, which always
executes with the same "this":

callAsyncScoped(qx.lang.Function.bind(handler, this),arg1,arg2,....)

best Fabian


> While qooxdoo is hurling towards 1.0 there is this question that is
> bugging me.
>
> I have learned, that using anonymous functions leads to grief when
> debugging. So instead of
>
>   server.callAsync(function(ret,exc,id){
>      \\ handler code
>   },arg1,arg2,arg3);
>
> I am writing
>
>   function handler(ret,exc,id){
>      \\ handler code
>   }
>
>   server.callAsync(handler,arg1,arg2,arg3);
>
> unfortunately handler runs out of scope. I can therefore not
> access 'this' inside the handler. Unless I pass this into the
> handler with the var that=this trick.
>
> It works fine but it makes it impossible (afaik) to setup handlers
> as private methods in the object.
>
> In other methods like addListener it is possible to pass along the
> scope the handler should be run in.
>
> I would realy like to see a callAsyncScoped call that works similar
> to addListener:
>
>   callAsyncScoped(handler,scope,arg1,arg2,....)
>
> cheers
> tobi
>
>  


--
Fabian Jakobs
JavaScript Framework Developer

1&1 Internet AG - Web Technologies
Ernst-Frey-Straße 9 · DE-76135 Karlsruhe
Telefon: +49 721 91374-6784
[hidden email]

Amtsgericht Montabaur / HRB 6484
Vorstände: Henning Ahlert, Ralph Dommermuth, Matthias Ehrlich, Thomas Gottschlich, Robert Hoffmann, Markus Huhn, Hans-Henning Kettler, Dr. Oliver Mauss, Jan Oetjen
Aufsichtsratsvorsitzender: Michael Scheeren


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: about callAsync and scope

oetiker
In reply to this post by Noggin182
Today Matthew Gregory wrote:

> Hi Tobias,
> You can your qx.lang.Function.bind
>
> server.callAsync(qx.lang.Function.bind(handler, this), arg1, arg2...);
>
> That should fix your issue :)

cool ... so it becomes simpley a question of huffman coding or not
thanks

tobi

>
> HTH
> Matt
>
> Tobias Oetiker wrote:
> > While qooxdoo is hurling towards 1.0 there is this question that is
> > bugging me.
> >
> > I have learned, that using anonymous functions leads to grief when
> > debugging. So instead of
> >
> >   server.callAsync(function(ret,exc,id){
> >      \\ handler code
> >   },arg1,arg2,arg3);
> >
> > I am writing
> >
> >   function handler(ret,exc,id){
> >      \\ handler code
> >   }
> >
> >   server.callAsync(handler,arg1,arg2,arg3);
> >
> > unfortunately handler runs out of scope. I can therefore not
> > access 'this' inside the handler. Unless I pass this into the
> > handler with the var that=this trick.
> >
> > It works fine but it makes it impossible (afaik) to setup handlers
> > as private methods in the object.
> >
> > In other methods like addListener it is possible to pass along the
> > scope the handler should be run in.
> >
> > I would realy like to see a callAsyncScoped call that works similar
> > to addListener:
> >
> >   callAsyncScoped(handler,scope,arg1,arg2,....)
> >
> > cheers
> > tobi
> >
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> qooxdoo-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
>
>

--
Tobi Oetiker, OETIKER+PARTNER AG, Aarweg 15 CH-4600 Olten, Switzerland
http://it.oetiker.ch [hidden email] ++41 62 775 9902 / sb: -9900

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: about callAsync and scope

fritz
In reply to this post by Fabian Jakobs
The remaining question I have is:

Why are callAsync() and addListener() doing this differently (except perhaps
for historical reasons)?

Wouldn't it be more consistent if both would have a scope parameter?
Of course, this would be a backward-incompatible change ...

Cheers,
Fritz

On Mon, 23 Nov 2009, Fabian Jakobs wrote:

> Hi Tobias,
>
> one solution to your problem is using qx.lang.Function.bind. With this
> function you can create a wrapped version of your method, which always
> executes with the same "this":
>
> callAsyncScoped(qx.lang.Function.bind(handler, this),arg1,arg2,....)
>
> best Fabian
>
>
>> While qooxdoo is hurling towards 1.0 there is this question that is
>> bugging me.
>>
>> I have learned, that using anonymous functions leads to grief when
>> debugging. So instead of
>>
>>   server.callAsync(function(ret,exc,id){
>>      \\ handler code
>>   },arg1,arg2,arg3);
>>
>> I am writing
>>
>>   function handler(ret,exc,id){
>>      \\ handler code
>>   }
>>
>>   server.callAsync(handler,arg1,arg2,arg3);
>>
>> unfortunately handler runs out of scope. I can therefore not
>> access 'this' inside the handler. Unless I pass this into the
>> handler with the var that=this trick.
>>
>> It works fine but it makes it impossible (afaik) to setup handlers
>> as private methods in the object.
>>
>> In other methods like addListener it is possible to pass along the
>> scope the handler should be run in.
>>
>> I would realy like to see a callAsyncScoped call that works similar
>> to addListener:
>>
>>   callAsyncScoped(handler,scope,arg1,arg2,....)
>>
>> cheers
>> tobi
>>
>>
>
>
>

--
Oetiker+Partner AG tel: +41 62 775 9903 (direct)
Fritz Zaucker                        +41 62 775 9900 (switch board)
Aarweg 15                            +41 79 675 0630 (mobile)
CH-4600 Olten                   fax: +41 62 775 9905
Schweiz                         web: www.oetiker.ch

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: about callAsync and scope

Derrell Lipman
On Mon, Nov 23, 2009 at 06:15, Fritz Zaucker <[hidden email]> wrote:
The remaining question I have is:

Why are callAsync() and addListener() doing this differently (except perhaps
for historical reasons)?

Wouldn't it be more consistent if both would have a scope parameter?
Of course, this would be a backward-incompatible change ...

That's exactly the problem. It is a historical issue and the change would not be backward-compatible. I would not be opposed to a change, even non-backward-compatible, if the migration scripts handled it, I could make the change to the method but I don't have the time right now to work on the migration scripts.

Tobias, I would recommend creating a ticket for this. If someone is willing to write the migration scripts, I'll do the code changes. This will also need Andreas Ecker's approval, as it is a significant API change.

Derrell


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: about callAsync and scope

Noggin182
Wouldn't it be possible just to check the type of the second parameter?
If it is a derivitive of qx.core.Object use it as the context otherwise
pass it as the first parameter?

Derrell Lipman wrote:

> On Mon, Nov 23, 2009 at 06:15, Fritz Zaucker <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     The remaining question I have is:
>
>     Why are callAsync() and addListener() doing this differently (except
>     perhaps
>     for historical reasons)?
>
>     Wouldn't it be more consistent if both would have a scope parameter?
>     Of course, this would be a backward-incompatible change ...
>
>
> That's exactly the problem. It is a historical issue and the change
> would not be backward-compatible. I would not be opposed to a change,
> even non-backward-compatible, if the migration scripts handled it, I
> could make the change to the method but I don't have the time right now
> to work on the migration scripts.
>
> Tobias, I would recommend creating a ticket for this. If someone is
> willing to write the migration scripts, I'll do the code changes. This
> will also need Andreas Ecker's approval, as it is a significant API change.
>
> Derrell
>
>
> ------------------------------------------------------------------------
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> qooxdoo-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: about callAsync and scope

Derrell Lipman
On Mon, Nov 23, 2009 at 07:12, Matthew Gregory <[hidden email]> wrote:
Wouldn't it be possible just to check the type of the second parameter?
If it is a derivitive of qx.core.Object use it as the context otherwise
pass it as the first parameter?

It would, but I'd consider that to be a pretty nasty hack. That would mean that the arguments to the remote method begin at different locations in the parameter list depending on the value of that second parameter. It would also preclude the (granted, unlikely) possibility that one wants to pass an Object as an argument.

Derrell


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel