【问题标题】:How to make data storage unique to project in VSTS extension?如何在 VSTS 扩展中使数据存储对项目唯一?
【发布时间】:2018-08-08 21:21:16
【问题描述】:
    VSS.getService(VSS.ServiceIds.ExtensionData)
    // the callback on the next line returns a promise, which the JavaScript engine will follow, so you don't need to nest the next `then`
        .then((dataService) => dataService.getDocuments('MyCollection2'))
        .then((docs) => { ...

这就是我们在 VSTS 扩展中访问数据存储的方式。 MyCollection2 是我们正在使用的存储的名称。 然而,这并不是该项目独有的。当我尝试从同一组织内的另一个项目访问中心扩展时,我仍然可以看到数据。

我尝试根据我访问的项目动态命名集合,但没有明确的方法来获取扩展端的项目名称。

如何使数据存储在同一组织内的项目中唯一?

【问题讨论】:

    标签: azure-devops azure-devops-extensions


    【解决方案1】:

    您可以从getWebContext() 获取项目名称。然后将 DocumentId 值设置为 ProjectName:

    // Get data service
        VSS.getService(VSS.ServiceIds.ExtensionData).then(function(dataService) {
            // Set value (default is project collection scope)
            dataService.setValue("ProjectName", "DocumentId").then(function(value) {
                console.log("Key value is " + value);
            });
        });
    

    之后,检索设置值:

    // Get data service
        VSS.getService(VSS.ServiceIds.ExtensionData).then(function(dataService) {
            // Get value in user scope
            dataService.getValue("ProjectName").then(function(value) {
                console.log("User scoped key value is " + value);
            });
        });
    

    最后,当你从 DocumentId 中获取文档时,它会从一个项目中获取:

    // Get data service
        VSS.getService(VSS.ServiceIds.ExtensionData).then(function(dataService) {
            // Get document by id
            dataService.getDocument("MyCollection", "DocumentId").then(function(doc) {
                // Assuming document has a property named foo
                console.log("Doc foo: " + doc.foo); 
            });
        });
    

    【讨论】:

    • 感谢您的回答。这是有道理的,但每当我使用VSS.getWebContext() 时,它都会给我一个undefined。如果在VSS.init(); 之后我做了
    • 我也尝试过使用__vssPageContext。当我尝试从 VSTS 页面对其进行控制台时,这是可以访问的,但是当我将其放入 console.log(__vssPageContext) 之类的代码中时,它会显示为 undefined
    • 放在vss.ready:VSS.ready(function () {VSS.GetWebContext });怎么样?
    • 董仍然说未定义。奇怪的是,当我将它放入 onclick 函数时它会起作用。但我想在加载时使用它。
    • @Dawn17 你在哪里使用扩展并调用 getWebContext()?比如构建摘要或工作中心等等?
    【解决方案2】:

    我实际上通过使用 Promise 解决了这个问题。

        // Sets unique data storage name for each project
        function setDBName() {
            return new Promise(function(resolve, reject) {
    
                    VSS.ready(function(){
                        let web = VSS.getWebContext();
                        resolve(web);
                    })
            })
        }
    

    然后

        setDBName()
            .then(function(res){
                console.log('res', res)
            }
            .catch(function(err){
                console.log('err', err)
            }
    

    这将在 VSS 准备好获取时返回 Web 上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      相关资源
      最近更新 更多