我对Javascript和VSTS扩展创建非常陌生,我希望创建一个扩展来计算VSTS中工作项的修订计数。为此,我正在尝试使用工作项表单扩展名中的示例Java脚本。在Java脚本中,我无法获取修订计数的当前值,并为其分配变量以使每个状态转换都递增。
从VSTS,我发现此方法可获取字段值,但是如何实现它getFieldValues("Custom.RevisionCount", returnOriginalValue);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Work item form page sample</title>
</head>
<body style="overflow:auto;">
<script src="node_modules/vss-web-extension-sdk/lib/VSS.SDK.min.js"></script>
<script>
VSS.init({
explicitNotifyLoaded: true,
usePlatformScripts: true,
//usePlatformStyles: true
});
//VSS.ready(function () {
VSS.require(["TFS/WorkItemTracking/Services"], function (_WorkItemServices) {
// VSS.require(["VSS/Service", "TFS/Core/RestClient"], function(VSS_Service, Tfs_Core_WebApi) {
// var client = VSS_Service.getCollectionClient(Tfs_Core_WebApi.CoreHttpClient4);
// Get the WorkItemFormService. This service allows you to get/set fields/links on the 'active' work item (the work item
// that currently is displayed in the UI).
function getWorkItemFormService()
{
return _WorkItemServices.WorkItemFormService.getService();
}
var project=VSS.getWebContext().project.name;
var teamid=VSS.getWebContext().team.id;
// Register a listener for the work item page contribution
VSS.register(VSS.getContribution().id, function () {
return {
// Called when the active work item is modified
onFieldChanged: function(args) {
if(project=="TFS_Training")
{
//alert(args);
var data=JSON.stringify(args);
var a2=JSON.parse(data);
var a3 = a2["changedFields"];
//var i = getFieldValues("Custom.RevisionCount", returnOriginalValue);
getWorkItemFormService().then(function(service)
//{
// Get the current values for a few of the common fields
// service.getFieldValues("Custom.RevisionCount").then(
// function (value) {
// $(".events").append($("<div/>").text("onLoaded - " + JSON.stringify(value)));
// });
var i = 0
if(a3.hasOwnProperty("System.State"))
{
function myFunction(i) {
return i+1;
}
getWorkItemFormService().then(function (service)
{
{
//service.setFieldValue("System.Title", "Requirment changed");
service.setFieldValue("Custom.RevisionCount", myFunction(i));
}
// errorMessage="statechanged";
// service.setError(errorMessage);
});
}
}
},
}
});
VSS.notifyLoadSucceeded();
});
//});
</script>
</body>
</html>
每个状态转换仍显示值1
最佳答案
您不需要定义i,可以直接在setFieldValue中使用value。从workitem服务中获取值之后。此类(不熟悉js部分),请根据需要更改代码:
getWorkItemFormService().then(function(service)
{ // get work item service first
function getWorkItemFormService()
{
return _WorkItemServices.WorkItemFormService.getService();
}
// Get the current values for a few of the common fields
// get filed value here then use below function
function (value) {
// $(".events").append($("<div/>").text("onLoaded - " + JSON.stringify(value)));
//service.setFieldValue("System.Title", "Requirment changed");
service.setFieldValue("Custom.RevisionCount", value+1;)
}
}
更多详细信息,请参见此官方教程:setFieldValue()
关于javascript - 如何将getFieldValues(“Custom.RevisionCount”,returnOriginalValue)分配给VSTS自定义扩展html文件中的变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57423321/