来自二级对象的 Firebase $id [英] Firebase $id from second level Object

查看:20
本文介绍了来自二级对象的 Firebase $id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在firebase中有这个例子

So I have this for example in firebase

clients {
   //clients with unique keys {
       invoices: {
         // Invoices with unique Keys
       }
   }
}

我用一个 ref 返回所有这些:

I'm returning all this with one ref like so:

.controller('singleClientController', function($scope, $firebaseObject, fbUrl, $routeParams) {

    var id = $routeParams.id;

    var singleRef = new Firebase(fbUrl+'/clients/'+id);
    var client = this;

    client.details = $firebaseObject(singleRef);

})

所以在我的 html 中,我试图为客户和发票返回 $id.我能够让客户使用 {{client.details.$id}} 没有问题,但是当我尝试使用发票 ID {{invoice.$id} 做同样的事情时} 我什么也没得到.

so in my html, I'm trying to return the $id for both the client and the invoice. I am able to get the client no problem with {{client.details.$id}} but when I try to do the same thing with the invoice id {{invoice.$id}} I don't get anything.

发票通过 foreach 显示,如下所示:

The invoices are displayed through a foreach like so:

<tr ng-repeat="invoice in client.details.invoices">
     <td>
         <a href="#/invoices/details/{{invoice.$id}}/{{client.details.$id}}">
            {{invoice.settings.number}}
         </a>
     </td>
     ...
</tr>

是不是因为发票在客户里面?如果是这样,您将如何返回发票的 ID?快把我逼疯了!请帮忙!

Is it because the invoices are inside the client? If so, how would you return the id for the invoices? It's driving me nuts! Please help!

为了更好地了解我的 firebase 设置,这里是我正在谈论的内容的屏幕截图.

For a better understanding of my firebase set-up here is a screenshot of what I'm talking about.

推荐答案

tl;dr - 在 Firebase 中,拥有扁平数据结构是理想的选择.

tl;dr - In Firebase it is ideal to have a flat data structure.

JSBin 示例

在您的情况下,您的客户下嵌套了发票.

In your case you have invoices nested under clients.

{
   "clients": {
      "1": {
         "name": "Alison",
         "invoices": {
            "0001": {
              "amount": 500
              "paid": true
            }
         }
      }
   }
}

这感觉很自然,因为发票在适当的客户下很好地分组.但是,这可能会导致与 Firebase 同步数据的性能不佳.Firebase 读取节点下的所有数据.

This feels natural because the invoices are nicely grouped underneath the proper client. However, this can lead to bad performance in syncing data with Firebase. Firebase reads all data underneath a node.

这意味着每次您阅读 /clients/1 时,您还会获得通过网络下载的发票.即使你只需要客户的名字,你也会得到发票.

This means every time you read /clients/1 you also get the invoices downloaded over the network. Even if you just need the client's name, you'll also get the the invoices.

解决方案是扁平化您的数据结构.

{
   "clients": {
      "1": {
        "name": "Alison"
      }
   },
   "clientInvoices": {
     "1": {
        "0001": {
          "amount": 500
          "paid": true
        }
     }
   }
}

这里要掌握的重要部分是共享密钥.

在本例中,密钥是 1.这是为了简单起见.实际上,您可能会使用 .push() id.

In this example the key is 1. This is for simplicity. In reality you'll likely use a .push() id.

通过使用此模式,您仍然可以通过简单地知道客户的密钥来检索客户的所有发票.这也使客户与发票分离.

By using this pattern you'll still be able to retrieve all of the invoices for the client by simply knowing their key. This also decouples the client from the invoices.

作为额外的好处,您的控制器代码和 ng-repeat 会更容易.

As an added benefit your controller code and ng-repeat will be much easier.

在您的情况下,您应该将发票从 $firebaseObject 切换到 $firebaseArray.我们甚至可以创建一个辅助工厂,通过客户的 id 获取发票.

In your case you should switch from a $firebaseObject to a $firebaseArray for the invoices. We can even create a helper factory that gets the invoices by a client's id.

.factory('invoices', function(fbUrl, $firebaseArray) {
   return function(clientId) {
      var ref = new Firebase(fbUrl).child('clientInvoices').child(clientId);
      return $firebaseArray(ref);
   }
})

// While we're at it, lets create a helper factory for retrieving a
// client by their id
.factory('clients', function(fbUrl, $firebaseObject) {
    return function(clientId) {
       var ref = new Firebase(fbUrl).child('clients').child(clientId);
       return $firebaseObject(ref);
    }
})

现在将辅助工厂注入您的控制器并使用 $routeParams.id 来检索客户的发票:

Now inject the helper factories into your controller and use the $routeParams.id to retrieve the client's invoices:

.controller('singleClientController', function($scope, $routeParams, invoices, clients) {

    $scope.client = clients($routeParams.id);
    $scope.clientInvoices = invoices($routeParams.id);

})

现在将它绑定到您的模板是轻而易举的:

Now binding it to your template is a breeze:

<tr ng-repeat="invoice in clientInvoices">
     <td>
         <a href="#/invoices/details/{{invoice.$id}}/{{client.$id}}">
            {{invoice.settings.number}}
         </a>
     </td>
     ...
</tr>

这篇关于来自二级对象的 Firebase $id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆