I have two events and I want them both to trigger the same code. The obvious answer is to have each event call the same function but i cant figure out how to do this. For example, say i am using the esri search dijit s:
on(s, "search-results", function(evt) {
console.log('event fired');
test(evt);
});
I have confirmed that the event gets fired but the function test is not found. Is this a scope issue? Any ideas? Thanks!
-Stefan
Solved! Go to Solution.
Stefan,
The issue seems to be with the use of the name "test" for the function name. Once I changed it to test2 it worked. I removed a bunch of legacy style coding you had mixed in with your AMD style also.
Stefan,
Yes it is a scope issue. Here is how to handle it.
Add a require for lang and then this code.
on(s, "search-results", lang.hitch(this, function(evt) { console.log('event fired'); test(evt); }));
Thanks for the quick response! I added:
"dojo/_base/lang" and lang in the require/function sections, tested and the page was able to load. It was not able to load when I only added "lang" and lang.
I then added the event and function code and got the message "Type error: test is not a function" in the console. Any idea as to what I am doing wrong? Thanks!
-Stefan
Stefan,
I would have to see more of your code as using lang.hitch should work for putting the results functions scope where it would know that test is a function unless you have the test function inside a different code block or another functions scope.
Robert, the code is attached- sorry it's pretty messy right now. I was able to incorporate an event and function to one of ESRI's select samples and your suggestion worked so there is something definitely wrong with my code. The event fires when you choose an item from the select tool. If you type 'sound' in the search box several results will appear so just pick one of those. I really appreciate your help!
-Stefan
Thanks so much Robert! I really appreciate it!
-Stefan