如何从另一个方法的函数内部调用Dojo方法?

slhcrj9b  于 2022-12-16  发布在  Dojo
关注(0)|答案(2)|浏览(163)

我觉得我错过了一些基本的东西。
我想让_webCall()方法运行一次,所以我设置了这个IIFE函数来运行fireFunc(),它调用_webCall()方法,但它一直说this._webCall(url)不是一个函数。
我不知道我做错了什么。我怎么能从函数内部调用_webCall()呢?

_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();
      },

字符串

jmp7cifd

jmp7cifd1#

在dojo中有一个模块有hitch()函数,它是专门为您的情况:
首先导入"dojo/_base/lang",然后使用lang.hitch()函数指出执行的上下文(引用您的模块)

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();
  },

});
s4n0splo

s4n0splo2#

下面是不使用hitch而使用bind的方法

onOpen: function(){
        const webCallFnc = this._webCall.bind(this);
        const fireFunct = (() => {
            const url = 'someurl';
            let executed = false;
            return () => {
                if(!executed){
                    executed = true;
                    webCallFnc(url);
                }
            }
        })();
        fireFunct();
    },

相关问题