Select to view content in your preferred language

Same code to be triggered by more than one event

2880
6
Jump to solution
12-21-2015 02:32 PM
StefanCoe
Emerging Contributor

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

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

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.

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus

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);
}));
StefanCoe
Emerging Contributor

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

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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.

0 Kudos
StefanCoe
Emerging Contributor

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

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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.

0 Kudos
StefanCoe
Emerging Contributor

Thanks so much Robert! I really appreciate it!

-Stefan

0 Kudos