So I had always hoped to avoid the whole dojo universe. The JSAPI 4.x seems to more and more inoculate against the inner workings of whatever dojo its using. But now, I'm trying to build some custom WAB widgets and see myself having unscramble some of this dojo stuff. i got a 2-part question, and question subject refers to only part 2.
Part 1
Robert Scheitlin, GISP had some advice suggesting to work with dojo attachments points, as in ...
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>input</SPAN> <SPAN class="attr-name token">data-dojo-attach-point</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>myButton<SPAN class="punctuation token">"</SPAN></SPAN> <SPAN class="attr-name token">type</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>button<SPAN class="punctuation token">"</SPAN></SPAN> <SPAN class="attr-name token">value</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>Click Here<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
so that in my code I could refer to this element in widget.html simply as this.myButton.
But I'm still not sure when to use the above or - provided I have added id = 'myButton', use this;
dom<SPAN class="punctuation token">.</SPAN><SPAN class="token function">byId</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"myButton"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
I see folks using them almost interchangeably through this forum and elsewhere. Are there some simple guiding principles?
Part 2
Now for the Select. I'm making a call to a web service, ignoring geometry and just returning the feature's name. Then I populate a <select> with that, which I have called:
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>select</SPAN> <SPAN class="attr-name token">id</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">'</SPAN>results<SPAN class="punctuation token">'</SPAN></SPAN> <SPAN class="attr-name token">data-dojo-attach-point</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">'</SPAN>results<SPAN class="punctuation token">'</SPAN></SPAN> <SPAN class="attr-name token">data-dojo-type</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>dijit/form/Select<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>select</SPAN><SPAN class="punctuation token">></SPAN></SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
The following code populates a bunch of entries (or <option>'s) in that <select> with records from the service.
<SPAN class="comment token">//Interate over features in featureset</SPAN>
<SPAN class="comment token">//find desired feature attribute's value</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> c <SPAN class="operator token">=</SPAN> document<SPAN class="punctuation token">.</SPAN><SPAN class="token function">createElement</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'option'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
c<SPAN class="punctuation token">.</SPAN>innerHTML <SPAN class="operator token">=</SPAN> myAttributeValue<SPAN class="punctuation token">;</SPAN>
c<SPAN class="punctuation token">.</SPAN>value <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">this</SPAN><SPAN class="punctuation token">.</SPAN>results<SPAN class="punctuation token">.</SPAN><SPAN class="token function">appendChild</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// 'results' being my dojo attach point</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Next, I would like to use the dijit's event listener to watch if one of those <options>'s is selected. Again, ...
<SPAN class="token function">on</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">this</SPAN><SPAN class="punctuation token">.</SPAN>results<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"change"</SPAN><SPAN class="punctuation token">,</SPAN>lang<SPAN class="punctuation token">.</SPAN><SPAN class="token function">hitch</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">this</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN>evt<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN>
console<SPAN class="punctuation token">.</SPAN><SPAN class="token function">log</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">this</SPAN><SPAN class="punctuation token">.</SPAN>results<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>The above gets me a reference to the <select> but no attempt - such as ".value",".displayValue", "_getDisplayedValueAttr()" or ".selected" has been successful in getting me the value that's selected, and I've scoured the Dojo site for info. I can iterate over this.results.options and check the attribute selected for each one, but is there a simpler way? Thanks.