Select to view content in your preferred language

toggle button change and pass a variable

640
3
Jump to solution
06-29-2018 12:58 PM
BrianLomas
Occasional Contributor III

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 + "...")
          }
     }
     
}
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

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.

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

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?

0 Kudos
BrianLomas
Occasional Contributor III

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(){
          }

          }
     }
     
}
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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.

0 Kudos