0

我正在关注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 显式添加为字段。

4

1 回答 1

2

那么对于您问题的第一部分,索引不会改变结果的样子,只会改变您查询数据的方式。因此,使用“id”字段创建索引不会满足您的要求。如果要更改结果,则需要使用变压器。因此,在您的情况下,您将创建一个变压器,例如:

from product in results
select new
{
   id = product.Id,
   name = product.Name,
   category = product.Category,
   description = product.Description,
   price = product.Price
}

比您需要将变压器连接到您的索引。所以你的网址看起来像这样:

http://localhost:8080/databases/Catalogue/indexes/dynamic/Products?resultsTransformer=ProductTransformer

希望有帮助。

如果您不想使用变压器,作为替代建议。在角度你可以做这样的事情:

<table>
   <tr ng-repeat="product in products">
      <td>{{getId(product)}}</td>
      <!-- other columns -->
   </tr>
</table>

然后在你的 $scope 上:

$scope.getId = function(product){
    return product["@metadata"]["@id"];
}
于 2014-10-10T04:07:25.253 回答