我正在尝试弄清楚如何应用promises基础设施,在这种情况下,我循环浏览项目组合项目列表以提取数据以供高级图表使用,并需要从相关用户故事中检索数据。目标是创建一种方法,在完成所有异步回调之前不尝试实例化图表对象。这是一个代码片段,说明了我正在尝试做的事情 - 在这个例子中,getCollection()方法的回调函数在检索完所有外部循环特征之后才被执行。
_.each(records, function(feature) {
var rname = (feature.get('Release')) ? feature.get('Release')._refObjectName: "None";
//various other feature record extraction steps
feature.getCollection('UserStories').load({
fetch: ['FormattedID', 'Name', 'InProgressDate','AcceptedDate'],
callback: function(stories, operation, success){
Ext.Array.each(stories, function(story) {
var storyname = story.get('FormattedID') + ': ' + story.get('Name');
// other chart data extraction steps
}
});
}
});
});
答案 0 :(得分:0)
我查看有关使用promises的文档中的指南,尤其是有关检索分层数据的部分:
https://help.rallydev.com/apps/2.0/doc/#!/guide/promises-section-retrieving-hierarchical-data
基本思想是保留表示子集合存储加载的所有promise的数组,然后使用Deft.Promise.all在继续之前等待它们全部完成。
基于上面的代码,这样的事情是这样的:
var childStoryPromises = [];
_.each(records, function(feature) {
var rname = (feature.get('Release')) ? feature.get('Release')._refObjectName: "None";
// various other feature record extraction steps
var userStoriesStore = feature.getCollection('UserStories');
var promise = userStoriesStore.load({
fetch: ['FormattedID', 'Name', 'InProgressDate','AcceptedDate']
});
childStoryPromises.push(promise);
feature.userStoriesStore = userStoriesStore; //save reference for later
});
//wait for all stores to load
Deft.Promise.all(childStoryPromises).then({
success: function() {
_.each(records, function(feature) {
var userStories = feature.userStoriesStore.getRange();
//extract user story data here
});
},
scope: this
});