2014-03-01 44 views
1

我在使用requirejs在不同模块循环中调用函数时遇到问题。循环中的函数调用驻留在模块A中,并在模块B中执行一个使用jQuery触发Ajax请求的函数。循环的每次迭代都会引发一个不同的请求,并将不同的参数传递给模块B的函数,该函数将触发Ajax请求。执行Ajax请求的成功函数时,我发现所有的参数值始终是最后一次Ajax调用的值,适用于所有4个独立的Ajax调用。使用requirejs循环中的函数

我做了一些Google搜索,听起来像这是在循环内执行函数时很常见的问题。修复倾向于将函数调用分解为不同的函数,从而创建不同的范围。由于我的循环和Ajax调用位于两个不同的模块中,我假设这可以解决该问题,但它仍然存在。

我已经尝试了其他堆栈溢出帖子中的一些解决方案,如: JSlint error 'Don't make functions within a loop.' leads to question about Javascript itselfHow to pass parameter to an anonymous function defined in the setTimeout call?没有成功。任何人有任何想法?

的回路模块示例代码:适合于Ajax模块B

define(["mpos"], 
    function(mpos){ 

     var monitor = { 
      startMonitoring : function(poolObj){  
       // Start Monitoring 
       $.each(mpos.msgs, function(action,callback){ 
        poolObj.action = action; 
        mpos.sendApiRequest(poolObj,action,callback); 

       }); 
      } 
     }; 

     return monitor; 
    } 
); 

示例代码 - 这个模块被引用在模块MPOS一个

define(["mule","constants"], 
function(mule,constants){ 

    var mpos = { 
     sendMessage : function(postData,callback,$poolOut){ 

      return $.ajax({ 
       'type':'post', 
       'url':constants.URLS.proxy, 
       'data':{'url':postData}, 
       success : function(data){ 
        // if we have $poolOut we know this is a mpos call 
        if($poolOut != undefined){ 
         var keys = Object.keys(data); 
         // add poolOut to data 
         data.poolOut = $poolOut; 

         var poolObj = $poolOut.data('poolObj'); 
         if(poolObj){ 
          var action = poolObj.action; 
          console.log(poolObj,action); 
          if(action){ 
           if(action == "getuserstatus"){ 
            mule.registerPool(poolObj); 
           } 
          } else { 
           log.error("No action on poolObj while attempting to calculate the need for a registerPool call"); 
          } 
         } 
        } 
        // parse data 
        callback.apply(this, data); 
       }, 
       error : function(x,h,r){ ... }, 
       dataType : 'json' 
      }); 
     }, 
     sendApiRequest : function(poolObj,action,callback){ 
      var url = poolObj.url + '&page=api&action=' + action; 

      var $poolOut = constants.cache.monitorOutput.find('.pool-out.' + poolObj.id); 

      var dfd = mpos.sendMessage(url,callback,$poolOut); 

      $.when(dfd).always(function(){ 
       var refreshTimer = setTimeout(function(){ 
        if(constants.state.monitorEnabled){ 
         mpos.sendApiRequest(poolObj, action, callback); 
        } 
       }, poolObj.refreshRate); 

      }); 
     }, 
     msgs : { 
      "getuserstatus" : function(data){ ... }, 
      "getpoolstatus" : function(data){ ... }, 
      "getuserworkers" : function(data){ ... }, 
      "getuserbalance" : function(data){ ... } 
     } 
    }; 

    return mpos; 
} 
); 

谢谢!

+0

您是否试图在相同模块下复制您的非工作代码?我想问题是你的代码,而不是与requirejs,但也许我错了... – inf3rno

回答

1

注意:我假设$poolOut.data('poolObj')正用于查找实例传递给startMonitoring,并将每次返回相同的实例。

您声明:“循环的每次迭代都会触发不同的请求,并将不同的参数传递给模块B的函数,该函数将触发Ajax请求。”

此声明不正确。每次迭代都会触发不同的请求,其中第一个参数poolObj是每个迭代中的相同的

在您的.each迭代中,在每次调用sendApiRequest之前覆盖poolObj.action的值。

在AJAX成功处理程序中,有可能在之后调用所有迭代都已完成,则poolObj.action的值将具有您在上次迭代中设置的值。

为了解决这个问题,我想你也需要将action作为参数传递给sendMessage,这样每个函数调用的闭包中都会存储一个单独的值。

var mpos = { 
    sendMessage : function(postData,action,callback,$poolOut){ 

     return $.ajax({ 
      'type':'post', 
      'url':constants.URLS.proxy, 
      'data':{'url':postData}, 
      success : function(data){ 
       // if we have $poolOut we know this is a mpos call 
       if($poolOut != undefined){ 
        var keys = Object.keys(data); 
        // add poolOut to data 
        data.poolOut = $poolOut; 

        var poolObj = $poolOut.data('poolObj'); 
        if(poolObj){ 
         // action is not guaranteed to be the same as poolObj.action here, 
         // since poolObj.action may have changed since this function was first called 
         console.log(poolObj,action); 
         if(action){ 
          if(action == "getuserstatus"){ 
           mule.registerPool(poolObj); 
          } 
         } else { 
          log.error("No action on poolObj while attempting to calculate the need for a registerPool call"); 
         } 
        } 
       } 
       // parse data 
       callback.apply(this, data); 
      }, 
      error : function(x,h,r){ ... }, 
      dataType : 'json' 
     }); 
    }, 
    sendApiRequest : function(poolObj,action,callback){ 
     var url = poolObj.url + '&page=api&action=' + action; 

     var $poolOut = constants.cache.monitorOutput.find('.pool-out.' + poolObj.id); 

     var dfd = mpos.sendMessage(url,action,callback,$poolOut); 

     $.when(dfd).always(function(){ 
      var refreshTimer = setTimeout(function(){ 
       if(constants.state.monitorEnabled){ 
        mpos.sendApiRequest(poolObj, action, callback); 
       } 
      }, poolObj.refreshRate); 

     }); 
    }, 
    msgs : { 
     "getuserstatus" : function(data){ ... }, 
     "getpoolstatus" : function(data){ ... }, 
     "getuserworkers" : function(data){ ... }, 
     "getuserbalance" : function(data){ ... } 
    } 
}; 
+0

是的,我有一个大规模的逻辑问题在这里。试图在每次迭代中重复使用相同的obj,就像它是一个新的obj一样。不知道我是如何错过这个小时和小时。感谢您的意见,帮了大忙! – ShortRound1911