Data source query callback problems (call order, ability to change global variables)

I shortened my code to the following short example. In it, I want to request a set of iterations, and then in the callback, iterate over the iterations and summarize the resources. There is a global variable in which I would like to store the amount ... but I cannot get this to work.

The specific problem is that the request (and the associated callback) is launched after another 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> 
+4
source share
1 answer

It may be a little tricky to get asynchronous callbacks, but you're really close. Basically, if you just move your table creation from Main to the top of your ProcessIterations callback, you should be fine:

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

This way, you won’t display your table until the data is available from your rallyDataSource.findAll call and your SumSource has been calculated.

As an additional check of resources, some examples from our reference documentation on using RallyDataSource and asynchronous callbacks: http://developer.rallydev.com/help/rally-data-source

+1
source

Source: https://habr.com/ru/post/1379686/


All Articles