I'm new to javascript so I'm hoping you can help me along. I'm working on a widget that has a getlocation function loop running at startup. My hope is to have this toggle button be able to change a variable and send it to another function. I'm having trouble getting the variable to be passed into function c below. Ultimately the message is sent via POST. The code below is the basics of what I'm trying to do. The actual code is very long but I think you can get the idea from the snippet. Thanks.
begin: function a(){
var variable = this.variable
new ToggleButton({
onchange: function{
if val(){
label x
this.variable = x
}else{
label y
this.variable =y
}
}
}, "button id").startup();
function b(){
function c(){
message = ("..."+ this.variable + "...")
}
}
}
Solved! Go to Solution.
Brian,
It is still hard to tell from such generalized code, but it looks like it’s a code scope issue. The this keyword has a different scope when placed inside other functions that return a deferred. To get past this you use dojo lang.hitch.
Brain,
You code example is maybe to vague and generalized. Why does function b have a function c nested inside it? How does function c ever get called?
Hey Robert,
Function b actually has 3 functions nested in it. One is the getlocation and function c is the showlocation of a geolocation. The showlocation is also compling the message to be sent. The 3rd nested function is an ajax request to post the message. So, function b is called and the nested functions are called within b. I hope that makes sense.
begin: function a(){
var variable = this.variable
new ToggleButton({
onchange: function{
if val(){
label x
this.variable = x
}else{
label y
this.variable =y
}
}
}, "button id").startup();
function b(){
function getlocation(){
calls showlocation... sends location
}
function showlocation(){
message = ("..."+ this.variable + "...")
}
function sendobject(){
$ajax(){
}
}
}
}
Brian,
It is still hard to tell from such generalized code, but it looks like it’s a code scope issue. The this keyword has a different scope when placed inside other functions that return a deferred. To get past this you use dojo lang.hitch.