3
<html>
<head>    
<script>    
    require(["dojo/ready"],function(ready){

      function init(dataItem) {
          alert("inside init method")
          updateData(dataItem);
      }
      function updateData(dataItem){
       //inside this function, i am creating some breadcrumb and making each part of it a link.
       //Now that link is calling another method outerFunction()
      }
      ready(function(){
          init({
              type: "All Locations",
              id: "All_Locations"
          });
      });
   });

function outerFunction(jsonObj){
    init(jsonObj); // not working

}
</script>
</head>
<body>
   <div>
    </div>
</body>
</html>

So basically i need to call init() from outerFunction(). Please help me in this.

I need to call init() defined above , from outerFunction(). But this is not happening. Initially i tried to keep this outerFunction() inside require block. But then in that case, outerFunction() was not getting called from breadcrumb link.

So i had to move it outside. And now from outside, outerFunction() is getting called but init() is not getting called from outerFunction()

Or if in any way I can write outerFunction method inside require block and can make same call flow.

Solution: I have used dojo.subscribe() inside ready and dojo.publish() in outerFunction(). And it worked for me.

coder87
  • 101
  • 12
  • 1
    Possible Duplicate of http://stackoverflow.com/questions/9626821/dojo-amd-cant-call-a-function-inside-a-require?rq=1 It handles the same question you asked. – MiBrock Jun 23 '15 at 10:56
  • @MiBrock, thanks for the link you provided. It is indeed helpful. Although i solved it using publich/subscribe. I have detailed it in my question. – coder87 Jun 23 '15 at 12:45

1 Answers1

3

Well, it's indeed a scope issue. You can solve it in various ways. One of them is to put outerFunction() inside the require() block like you mentioned.

However, if you call that function from somewhere else, you might encounter a problem. For a breadcrumb link (you mentioned) you could use an onclick handler inside the same require() block, for example:

on(breadcrumbLink, "click", function() {
    outerFunction({});
});

However, as soon as the amount of code increases, you'll have a problem if you put it all into the same require() block (because it will become unmaintainable), so you could split the init() function to a separate module, which can be included for both functions.


A third solution, though it is quite a bad practice if it's done quite often, is to put either one of them on the window scope. If you move the init() function to window, for example:

window.init = init;

Then you can access it from within the outerFunction(). Another way to do this is to move the outerFunction() inside the require() block and adding it to window as well, for example:

require(/** Stuff */, function() {
    function init() { }

    function outerFunction() { }
    window.outerFunction = outerFunction;
});
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
  • 1
    Is there any problem with using your last solution with `window.outerFunction = outerFunction;` if used in a single page application in which there would be a need of accessing anywhere between 50 and 100 functions that are inside the dojo require block? – Pegues Apr 11 '16 at 23:49
  • @ScriptsConnect http://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice – g00glen00b Apr 12 '16 at 06:02