<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Custom widget variable scope in Web AppBuilder Custom Widgets Questions</title>
    <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821989#M7483</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Robert! This partially worked however. Interestingly, inside the layer-remove event (after layer-add has been fired once), I could access this.addedLayers, but not the variable I declared previously (i.e. var addedLayers = this.addedLayers). Why is that?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another question...if I declare the local variable 'addedLayers' without the 'var' (i.e. addedLayers = this.addedLayers), and without using lang.hitch, the variable is accessible in both events, as is the local map variable (also declared without a var). &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How does this work (both the scope and declaring variables without the 'var')? If it's too much, I'd also be happy if you could point me to a good resource that explains it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 18 Apr 2016 15:47:30 GMT</pubDate>
    <dc:creator>NartTamash</dc:creator>
    <dc:date>2016-04-18T15:47:30Z</dc:date>
    <item>
      <title>Custom widget variable scope</title>
      <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821987#M7481</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Not a seasoned developer, so this might have a very simple answer that I'm failing to see/understand.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm trying to build a custom widget from scratch, and most of my code gets executed when a button is clicked in the widget interface (it's a portal map services search). I am using a global variable that needs to be manipulated at various stages (i.e. events). To keep this as short as possible my code looks something like this:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;...&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;addedLayers: [], &lt;SPAN style="color: #505050;"&gt;//This is the global variable&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;postCreate: ...&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;startup: ...&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;customFunctionOnButtOnclick: function() {&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;map = this.map;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;var addedLayers = this.addedLayers;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on(map, "layer-remove", function(event) {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //do something to addedLayers&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on(map, "layer-add", function(event) {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //do something else to addedLayers&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;},&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;...&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a reason why addedLayers can be accessed in the layer-add event but not in the layer-remove event?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I obviously only included the relevant parts of that main function. There are other bits that do different things, but none of which should affect addedLayers. Also the two map events, are in the same succession as presented here.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 Apr 2016 13:08:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821987#M7481</guid>
      <dc:creator>NartTamash</dc:creator>
      <dc:date>2016-04-18T13:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: Custom widget variable scope</title>
      <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821988#M7482</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Nart,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Sure, it is a scoping issue as you have stated already. Each time you an event handler using dojo/on the scope of the handling function changes and you need to use lang.hitch to maintain the scope.&lt;/P&gt;&lt;P&gt;Add "dojo/_base/lang" require&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;customFunctionOnButtOnclick: function() {&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp; map = this.map;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp; var addedLayers = this.addedLayers;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp; ...&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp; on(map, "layer-remove", lang.hitch(this, function(event) {&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //do something to addedLayers&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp; }));&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp; on(map, "layer-add", lang.hitch(this, function(event) {&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //do something else to addedLayers&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&amp;nbsp; }));&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;},&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 09:45:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821988#M7482</guid>
      <dc:creator>RobertScheitlin__GISP</dc:creator>
      <dc:date>2021-12-12T09:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Custom widget variable scope</title>
      <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821989#M7483</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Robert! This partially worked however. Interestingly, inside the layer-remove event (after layer-add has been fired once), I could access this.addedLayers, but not the variable I declared previously (i.e. var addedLayers = this.addedLayers). Why is that?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another question...if I declare the local variable 'addedLayers' without the 'var' (i.e. addedLayers = this.addedLayers), and without using lang.hitch, the variable is accessible in both events, as is the local map variable (also declared without a var). &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How does this work (both the scope and declaring variables without the 'var')? If it's too much, I'd also be happy if you could point me to a good resource that explains it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 Apr 2016 15:47:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821989#M7483</guid>
      <dc:creator>NartTamash</dc:creator>
      <dc:date>2016-04-18T15:47:30Z</dc:date>
    </item>
    <item>
      <title>Re: Custom widget variable scope</title>
      <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821990#M7484</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Nart,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Using a the var makes a variable a local variable and not using var make the variable a global variable (this is not a good thing per-say).&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;addedLayers: [], //This is the global variable&lt;/P&gt;&lt;P&gt;postCreate: ...&lt;/P&gt;&lt;P&gt;startup: ...&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;above addedLayer is now a psuedo-global var that can be access using "this.addedLayer"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In WAB widget development if you want a var to be accessible in any function in the widget then you use addedLayers:[], like you have following the "return declare([BaseWidget, _WidgetsInTemplateMixin], {" line. And you access it using this.addedLayers but the function or arrays function loop has to maintain the same scope using lang.hitch.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There is absolutely no need to create a var for the map as all widgets have access to "this.map"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am not sure why you are having problem accessing "this.addedLayers" array in the on "layer-remove" if the function is preceded by lang.hitch.&lt;/P&gt;&lt;P&gt;on(this.map, "layer-remove", lang.hitch(this, function(event) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //do something to addedLayers&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; console.info(this.addedLayers);&lt;/P&gt;&lt;P&gt;}));&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 Apr 2016 16:50:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821990#M7484</guid>
      <dc:creator>RobertScheitlin__GISP</dc:creator>
      <dc:date>2016-04-18T16:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: Custom widget variable scope</title>
      <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821991#M7485</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sorry, I might not have been very clear...I was refering to the 'addedLayers' declared with a 'var' inside the 'customFunctionOnButtonClick' section as described in the first post, not the one declare before startup, postcreate, etc.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Apr 2016 05:39:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821991#M7485</guid>
      <dc:creator>NartTamash</dc:creator>
      <dc:date>2016-04-19T05:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: Custom widget variable scope</title>
      <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821992#M7486</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Nart,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;OK I am probably missing your question then. So here is a bit of advice.&lt;/P&gt;&lt;P&gt;Do not name your local vars and global vars the same. Because you do have them named the same it will get really confusing as to which one you are getting by using "this."&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;customFunctionOnButtOnclick: function() {
&amp;nbsp;&amp;nbsp;&amp;nbsp; //You don't need these next two lines as you can access the global versions of them by using "this."
&amp;nbsp;&amp;nbsp;&amp;nbsp; map = this.map;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var addedLayers = this.addedLayers;
...
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on(map, "layer-remove", lang.hitch(this, function(event) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //do something to addedLayers
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on(map, "layer-add", lang.hitch(this, function(event) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //do something else to addedLayers
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }));
},&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 09:46:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/custom-widget-variable-scope/m-p/821992#M7486</guid>
      <dc:creator>RobertScheitlin__GISP</dc:creator>
      <dc:date>2021-12-12T09:46:01Z</dc:date>
    </item>
  </channel>
</rss>

