我正在关注Pro AngularJS的教程,并且正在从 HTTP API 中检索一些产品。我不想使用Deployd,而是想使用RavenDB。
按照这篇文章的建议,我可以使用动态索引来获取所有产品,如下所示:
http://localhost:8080/databases/Catalogue/indexes/dynamic/Products
这导致 JSON 如下所示:
{
Results: [
{
name: "Kayak",
description: "A boat for one person",
category: "Watersports",
price: 275,
@metadata: {
Raven-Entity-Name: "Products",
@id: "products/1",
Temp-Index-Score: 0.882217,
Last-Modified: "2014-10-07T20:26:31.4436923Z",
Raven-Last-Modified: "2014-10-07T20:26:31.4436923",
@etag: "01000000-0000-0001-0000-000000000001"
}
},
{ ... }
AngularJS 的一个示例会导致:
$http.get(dataUrl)
.success(function(result) {
$scope.products = result.Results;
});
使用这种方法时,ID 存储为product["@metadata"].["@id"]
,在受影响的页面上绑定有点困难,而不是product.id
. 尝试显示 ID 时,将 ID 发回以从购物篮中删除,您将执行以下操作:
<table>
<tr ng-repeat="product in products">
<td>{{product["@metadata"]["@id"]}}</td>
<!-- other columns -->
</tr>
</table>
解决此问题的另一种方法是基本上动态创建 ID,如下所示:
$http.get(dataUrl)
.success(function (result) {
var localResults = [];
for (var i = 0; i < result.Results.length; i++) {
localResults.push(result.Results[i]);
localResults[i].id = result.Results[i]["@metadata"]["@id"];
};
$scope.data = {
products: localResults
};
});
我尝试创建显式调用 ID 的索引,例如:
// Index: Products/All
from product in docs.Products
select new {
id = product.Id,
name = product.Name,
category = product.Category,
description = product.Description,
price = product.Price
}
但是该文档的格式与以前完全相同。
我的问题是这样的:
RavenDB 是否可以仅发出文档中的字段,即使通过特定索引或转换器 a) 忽略元数据或 b) 将 ID 显式添加为字段。