【问题标题】:How to retrieve the ID as part of an index with AngularJS and RavenDB?如何使用 AngularJS 和 RavenDB 检索 ID 作为索引的一部分?
【发布时间】:2014-10-09 08:15:01
【问题描述】:

我正在学习来自 Pro AngularJS 的教程,并且正在从 HTTP API 检索一些产品。我不想使用Deployd,而是想使用RavenDB

按照this post 的建议,我可以使用动态索引来获取所有这样的产品:

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 显式添加为字段。

【问题讨论】:

    标签: angularjs ravendb ravendb-http


    【解决方案1】:

    对于您问题的第一部分,索引不会改变结果的样子,只会改变您查询数据的方式。因此,使用“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"];
    }
    

    【讨论】:

    • $scope.getId() 在这里听起来像是赢家。我可以把它放在服务上以减少重复吗?
    • 当然。不过,您可能希望从服务中提取数据并将该方法添加到所有返回的对象中。这样你就可以调用 {{product.getId()}} 任何一种方式都可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多