简体   繁体   中英

How to call Dojo method from inside function of another method?

I feel like I'm missing something fundamental here.

I want the _webCall() method to run once, so I have this IIFE function set up to run fireFunc() which calls the _webCall() method, but it keeps saying this._webCall(url) is not a function.

I can't figure out what I have done wrong here. How can I call _webCall() from inside a function?

 _webCall: function(url){
        console.log(url)
      },

  onOpen: function(){
        var fireFunct = (function(){
          var url = 'someurl';
          var executed = false;
          return returnFunc(){
            if(!executed){
              executed = true;
              this._webCall(url);
            }
          }
        })();
        fireFunct();
      },

In dojo there is a module that has the hitch() function , which is dedicated to your case :

first import the "dojo/_base/lang" , then use the lang.hitch() function to point out the context of execution to this (which refers to your module )

require([.., "dojo/_base/lang", ...], function(.., lang, ...){
  
  //...


  _webCall: function(url){
    console.log(url)
  },
  
   
  onOpen: function(){
      var fireFunct =
      lang.hitch( this ,(function(){ // <--- execute function in the context of the module , this will point to the module not IFII function context
            var url = 'someurl';
            var executed = false;
            return returnFunc(){
               if(!executed){
                  executed = true;
                  this._webCall(url);
               }
            }
         })()
      );
      fireFunct();
  },


});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM