English 中文(简体)
Data source query callback problems (call order, ability to change global variables)
原标题:

I ve reduced my code to the following short example. In it, I want to query for a set of iterations, and then in the callback, loop over the iterations and sum the resources. There is a global variable in which I d like to store the sum... but I can t get this to work.

The specific problem is that the query (and associated callback) are run after the other processing.

<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>
问题回答

It can be a little tricky to get the hang of asynchronous callbacks but you re really close. Basically if you just move the creation of your table from Main to up in your ProcessIterations callback you should be good:

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

That way you won t display your table until the data is available from your rallyDataSource.findAll call and your resourceSum has been calculated.

As an additional resource checkout some of the examples in our help documentation on using RallyDataSource and async callbacks: http://developer.rallydev.com/help/rally-data-source





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签