javascript – 数据源查询回调问题(调用顺序,更改全局变量的能力)

我已将代码缩减为以下简短示例.在其中,我想查询一组迭代,然后在回调中循环遍历迭代并对资源求和.有一个全局变量,我想存储总和…但我不能让它工作.

具体问题是查询(和相关的回调)在其他处理之后运行.

<html><!-- COMMENT -->

    <meta name="Name"    content="YOUR APP NAME HERE" /> 
    <meta name="Version" content="0.1" /> 
    <meta name="Vendor"  content="YOUR COMPANY NAME HERE" /> 

<!-- Rally SDK --> <script type="text/javascript" src="/apps/1.25/sdk.js"></script>

<!-- App script -->  <script> 

var rallyDataSource; var resourceSum = -1; 

function ProcessIterations(results) {
    alert("In ProcessIterations");  
    var resourceSum = 0; 
    for (iIter = 0; iIter < results.iterations.length; iIter++) {
      var iteration = results.iterations[iIter] ;
      resourceSum += iteration.Resources; 
    }
    alert("In ProcessIterations, resourceSum="+resourceSum);   }

function queryError () {
    alert("A query error occurred");   }

function runMainQuery() {   var today = dojo.date.stamp.toISOString(new Date(), {milliseconds: true, zulu: true});

  var queryObject = {
    key: "iterations",
    type: "Iteration",
    fetch: "Name,ObjectID,Resources,Project",
    order: "EndDate asc",
    query: "(Project.ObjectID != \"__PROJECT_OID__\")"
    };

  rallyDataSource.findAll(queryObject, ProcessIterations, queryError); }


function Main() {   rallyDataSource = new rally.sdk.data.RallyDataSource("__WORKSPACE_OID__", "__PROJECT_OID__",
                "__PROJECT_SCOPING_UP__", "__PROJECT_SCOPING_DOWN__");

  runMainQuery() ;

  var tableConfig = {
      'columnKeys'   : ['planEst'],
      'columnHeaders': ['Plan Estimate']   };   var table = new rally.sdk.ui.Table(tableConfig);   table.setCell(0,0,resourceSum) ;   table.display("app_div");  

}

rally.addOnLoad(Main);

</script> <body>
    <div id="app_div"></div> 
    <div id="error_div"></div>  </body> </html>

解决方法:

获得异步回调的挂起可能有点棘手,但你真的很接近.
基本上,如果您只是在ProcessIterations回调中将表的创建从Main移动到up,那么您应该很好:

function ProcessIterations(results) {
    alert("In ProcessIterations");  
    var resourceSum = 0; 
    for (iIter = 0; iIter < results.iterations.length; iIter++) {
      var iteration = results.iterations[iIter] ;
      resourceSum += iteration.Resources; 
    }
    alert("In ProcessIterations, resourceSum="+resourceSum);   }

    var tableConfig = {
      'columnKeys'   : ['planEst'],
      'columnHeaders': ['Plan Estimate']   };   
      var table = new rally.sdk.ui.Table(tableConfig);   
      table.setCell(0,0,resourceSum) ;   
      table.display("app_div"); 
}

function Main() {   
    rallyDataSource = new rally.sdk.data.RallyDataSource("__WORKSPACE_OID__", "__PROJECT_OID__",
            "__PROJECT_SCOPING_UP__", "__PROJECT_SCOPING_DOWN__");
    runMainQuery() ;
}

这样,在rallyDataSource.findAll调用数据可用并且您的resourceSum已经计算之前,您将不会显示您的表.

作为额外的资源,请查看我们的帮助文档中有关使用RallyDataSource和异步回调的一些示例:
http://developer.rallydev.com/help/rally-data-source

上一篇:Linux初记


下一篇:js 1+'2' == '1'+'2'