<?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: Help with scope issue using function in Web AppBuilder Custom Widgets Questions</title>
    <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/help-with-scope-issue-using-function/m-p/1182659#M14784</link>
    <description>&lt;P&gt;When using "this" in a function, you have to use "&lt;A href="https://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang-hitch" target="_self"&gt;lang.hitch&lt;/A&gt;" to make sure "this" is retaining the proper scope.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;geometryService.on("areas-and-lengths-complete", lang.hitch(this, outputAreaAndLength));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Jun 2022 12:43:50 GMT</pubDate>
    <dc:creator>KenBuja</dc:creator>
    <dc:date>2022-06-14T12:43:50Z</dc:date>
    <item>
      <title>Help with scope issue using function</title>
      <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/help-with-scope-issue-using-function/m-p/1182472#M14783</link>
      <description>&lt;P&gt;Having some difficulty with a seemingly simple scope issue that I am struggling to understand.&amp;nbsp; Someone will probably be able to quickly identify what I'm missing here!&lt;/P&gt;&lt;P&gt;I have a function that calculates planar area and calculates an acreage value with the geometry of a graphic as the input parameter.&amp;nbsp; In that process I intended to set "this.acrevalue" and have it set and available later on in other functions of a button click event.&amp;nbsp; However, it's always null BUT I can see that the correct acreage value is being calculated.&lt;/P&gt;&lt;P&gt;saveDataBtnClick() function is a button in the html of the widget that I hoped to somehow use the&amp;nbsp;getAcreageValueFromPolygon() function to get the acreage value before submitting a applyEdit() on the feature service.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;		getAcreageValueFromPolygon: function (geom) {
			projection.load().then(function (evt) {
				const inSpatialReference = new SpatialReference({ wkid: 102100 });
				const spSpatialReference = new SpatialReference({ wkid: 2881 });
				const poly = new Polygon(geom, inSpatialReference);
				var geoT = projection.getTransformation(inSpatialReference, spSpatialReference)
				const projectedPoly = projection.project(poly, spSpatialReference);

				var areasAndLengthParams = new AreasAndLengthsParameters();
				areasAndLengthParams.areaUnit = esri.tasks.GeometryService.UNIT_ACRES;
				areasAndLengthParams.lengthUnit = GeometryService.UNIT_FOOT;
				areasAndLengthParams.polygons = projectedPoly;

				var geometryService = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
				geometryService.on("areas-and-lengths-complete", outputAreaAndLength);
				geometryService.simplify([projectedPoly], function (simplifiedGeometries) {
					areasAndLengthParams.polygons = simplifiedGeometries;
					geometryService.areasAndLengths(areasAndLengthParams);
				});
			}.bind(this))

			function outputAreaAndLength(evtObj) {
				var result = evtObj.result;
				this.acrevalue = result.areas[0].toFixed(3)
				
			}
		},
		saveDataBtnClick: function () {

			var e = document.getElementById("layerTypeSelections");			
			//use config to reference services and specific service layers
			this.serverSideScratchLayer = new FeatureLayer(this.config.applicationFeatureLayer + "/" + lyrNum, { outFields: ["*"] });
			
			var features = this.unionedGraphic2.geometry
			if (features) {

				this.getAcreageValueFromPolygon(this.unionedGraphic2.geometry); //set the acreagae value and have it available for saving in applyEdits
				var spAcres = this.acrevalue; 
				attr["TRMT_ACRES"] = spAcres;

				var fYear = '1901'
				attr['FISCAL_YEAR'] = fYear
				this.unionedGraphic2["attributes"] = this.attr

				this.serverSideScratchLayer.applyEdits([this.unionedGraphic2], [], this.attr, lang.hitch(this, function (add) {
					if (add.length &amp;gt; 0) {
						if (add[0].objectId) {
							console.log('Add successful with objectid: ' + add[0].objectId);
							this.uploadOID = add[0].objectId
						}
                    }					
				}).bind(this));				
			}					
		},&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 20:47:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/help-with-scope-issue-using-function/m-p/1182472#M14783</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2022-06-13T20:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: Help with scope issue using function</title>
      <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/help-with-scope-issue-using-function/m-p/1182659#M14784</link>
      <description>&lt;P&gt;When using "this" in a function, you have to use "&lt;A href="https://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang-hitch" target="_self"&gt;lang.hitch&lt;/A&gt;" to make sure "this" is retaining the proper scope.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;geometryService.on("areas-and-lengths-complete", lang.hitch(this, outputAreaAndLength));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 12:43:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/help-with-scope-issue-using-function/m-p/1182659#M14784</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2022-06-14T12:43:50Z</dc:date>
    </item>
    <item>
      <title>Re: Help with scope issue using function</title>
      <link>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/help-with-scope-issue-using-function/m-p/1182681#M14785</link>
      <description>&lt;P&gt;I must have put that in just about every place except where you suggest. LOL&amp;nbsp; --- thank you, this resolves the issue (although not sure if it resolves the issue that I consistently struggle with scope).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Much appreciated!&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 13:19:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/web-appbuilder-custom-widgets-questions/help-with-scope-issue-using-function/m-p/1182681#M14785</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2022-06-14T13:19:21Z</dc:date>
    </item>
  </channel>
</rss>

