在VSTS扩展中加载JSON文件

时间:2018-03-15 19:57:45

标签: javascript azure-devops azure-devops-extensions

我正在尝试编写一个需要加载(和解析)JSON文件的VSTS扩展,但我很难找到正确的方法。

所以我有类似的东西:

VSS.init({
    explicitNotifyLoaded: true,
    usePlatformScripts: true
});

var witClient;
var rules;

VSS.ready(function () {
    require(["fs"], function (fs) {
        rules = JSON.parse(fs.readFileSync("urlMatches.json"));
    })

    VSS.require(["VSS/Service", "TFS/WorkItemTracking/RestClient"], function (VSS_Service, TFS_Wit_WebApi) {
        // Get the REST client
        witClient = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
    });

     // Register a listener for the work item page contribution.
    VSS.register(VSS.getContribution().id, function () {
        return {
            // Called after the work item has been saved
            onSaved: function (args) {
                witClient.getWorkItem(args.id).then(
                    function (workItem) {
                        // do some stuff involving the loaded JSON file...
                    });
            }
        }
    });

    VSS.notifyLoadSucceeded();
});

我尝试了很多变种而没有任何运气。我甚至尝试使用jQuery同步加载我的文件,但我的rules最终未定义。

所以我有文件urlmatches.json,我需要加载它并在我到达rules处理程序之前使用它来填充变量onSaved

1 个答案:

答案 0 :(得分:0)

您可以通过HTTP请求检索内容,例如:

 onSaved: function (args) {
                        console.log("onSaved -" + JSON.stringify(args));
                        var request = new XMLHttpRequest();
                        request.open('GET', 'TestData.txt', true);
                        request.send(null);
                        request.onreadystatechange = function () {
                            if (request.readyState === 4 && request.status === 200) {
                                var type = request.getResponseHeader('Content-Type');
                                console.log(request.responseText);
                            }
                        }