如何使 VSTS 扩展中的项目唯一的数据存储?
How to make data storage unique to project in VSTS extension?
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
是我们正在使用的存储的名称。
然而,这并不是该项目独有的。当我尝试从同一组织内的另一个项目访问中心扩展时,我仍然可以看到数据。
我尝试根据我访问的项目动态命名集合,但在扩展端没有明确的方法来获取项目名称。
如何使同一组织内的项目的数据存储唯一?
您可以从 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);
});
});
实际上我使用 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 准备好获取时,这将 return Web 上下文。
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
是我们正在使用的存储的名称。
然而,这并不是该项目独有的。当我尝试从同一组织内的另一个项目访问中心扩展时,我仍然可以看到数据。
我尝试根据我访问的项目动态命名集合,但在扩展端没有明确的方法来获取项目名称。
如何使同一组织内的项目的数据存储唯一?
您可以从 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);
});
});
实际上我使用 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 准备好获取时,这将 return Web 上下文。