在 Rally SDK 2 中,如何更新数组字段,例如在变更集的工件上

In Rally SDK 2, how do I update an array field, like on Artifacts for Changeset?

本文关键字:字段 更新 SDK Rally 何更新 数组      更新时间:2023-09-26

因此,我为变更集创建了一个模型,获取了一个包含 2 个工件的变更集并获取工件字段。 当我控制台记录时,我得到两个项目。 我有另一个项目(任务)我推到这个领域。 当我控制台记录工件数组时,我得到了三个项目。

但是当我直接或使用 set() 设置字段时,我控制台记录变更集,它仍然只认为有两个工件。 我可能做错了什么?

Rally.data.ModelFactory.getModel({
      type: 'Changeset',
      success: function(model) {
             model.load( '1234', {
                  fetch: [ 'Artifacts' ],
                  callback: function(result, operation) {
                            if ( operation.wasSuccessful() ){
                                var artifacts = result.get('Artifacts');
                                if ( ! artifacts ) {
                                    artifacts = [];
                                }
                                artifacts.push( item );
                                console.log( artifacts );
                                result.data.Artifacts = artifacts;
                                //result.set('Artifacts', artifacts);
                                console.log( result );
                                result.save( {
                                    callback: function( result, operation ) {
                                    console.log( "After saving: ", operation );
                                    }
                                    } );
                            }
                   }
             })
      }
});

看起来您需要创建一个新的工件数组,因为在使用相同的数组进行设置时,它不会将记录标记为脏(可能是 Rally 代码或 Ext 代码中的错误)。此外,还需要传递工件的数据对象(在下面的示例中为 newUserStory.data)。

我调整了您的代码,以创建一个创建存储库、变更集和用户情景的示例;然后将该用户情景附加到变更集的工件集合中。如果完整运行此代码,则需要将每个对象创建与更新代码分开运行,因为它是异步的,但彼此依赖。另一种选择是在每次 save() 调用中进行回调。

创建必要的域对象:

   var newRepo, newChangeset, newUserStory;
Rally.data.ModelFactory.getModel({
      type: 'SCMRepository',
      success: function(model) {
          newRepo = new model({
            Name: 'Repo1',
            SCMType: 'SCM Type 1'   
          });
          newRepo.save();
      }
});
Rally.data.ModelFactory.getModel({
      type: 'Changeset',
      success: function(model) {
          newChangeset = new model({
            CommitTimestamp: '2012-06-20 01:00:00',
            Revision: 'revision1',
            SCMRepository: newRepo.data._ref
          });
          newChangeset.save();
      }
});

Rally.data.ModelFactory.getModel({
      type: 'UserStory',
      success: function(model) {
          newUserStory = new model({
            Name: 'Test story ' + new Date()
          });
          newUserStory.save();
      }
});

现在更新变更集:

Rally.data.ModelFactory.getModel({
      type: 'Changeset',
      success: function(model) {
          model.load( newChangeset.data.ObjectID, {
                  fetch: [ 'Artifacts' ],
                  callback: function(result, operation) {
                      if ( operation.wasSuccessful() ){         
                            var artifacts = result.get('Artifacts'),
                                artifactsCopy = Ext4.Array.clone(artifacts);
                            artifactsCopy.push(newUserStory.data);
                            result.set('Artifacts', artifactsCopy);
                            result.save( {
                                callback: function( result, operation ) {
                                    if (operation.wasSuccessful()) {
                                        var artifactInUpdate = Ext4.Array.filter(result.data.Artifacts, function(artifact) {
                                            return artifact._ref === newUserStory.data._ref;
                                        });
                                        console.log('Userstory added to changeset:', artifactInUpdate.length > 0);
                                    } else { 
                                        console.log('update not successful');
                                    }
                                }
                            });
                        }
                  }
           });
      }
});

这是在周末发布的SaaS版本中修复的。 现在可以发送一个工件数组,该数组将替换原始列表,就像 WSAPI 中的其他数组一样。