Hi all,
This maybe slightly off-topic because this more a Javascript question, but
in the Qooxdoo context.
Suppose I have the following class A (pseudo-code) :
class A:
members :
response : null,
ask : function () { util,makeServerRequest
(this.setAndPrintResponse)},
setAndPrintResponse : function (resp) {
this.debug(resp);
this.response = resp;
}
The "ask" function sends the server a request asynchronously and when the
reply is received, prints it to the console and updates the instance
variable "response".
Unfortunately this doesn't work, because the makeServerRequest is part of a
static class and looks like this:
makeServerRequest : function (func){
var req = util.makeReq();
req.addListener("completed", function (e) {
func(e.getContent());
},this);
req.send();
}
And when "func" is called after the request is completed, it errors because
the current scope has no idea what the "this" in "this.debug" and
"this.response" refers to.
To fix this I have to thread a context through the calls like this:
class A:
members :
response : null,
ask : function () { util,makeServerRequest (this.setAndPrintResponse,
this)},
setAndPrintResponse : function (resp, context) {
context.debug(resp);
context.response = resp;
}
And change "makeServerRequest" to :
makeServerRequest : function (func,context){
var req = util.makeReq();
req.addListener("completed", function (e) {
func(e.getContent(),context);
},this);
req.send();
}
This seems somewhat inelegant. Is there some way of getting
"setAndPrintResponse" to understand that "this" always refers to this
instance?
thanks ...
-deech
------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel