正确的方法来模拟在火力地堡项目的集合地堡、火力、方法来、正确

2023-09-13 04:25:50 作者:自作多情必自毙°

在文档我看到很多使用索引值作为一个特定项目的关键名称的一部分的例子---但我不明白这是怎么到你的数据模型以一致的方式。

In the docs I see a lot of examples using index values as a part of the key name for a particular item --- but I don't understand how this is a consistent way to model your data.

例如,让我们说,我有一个文章列表:

For example let's say I have a list of articles:

https://gigablox.firebaseio.com/articles/
article1
article2
article3

当我准备添加第四条我知道我可以使用:

When I'm ready to add article4 I know I can use:

var length = Object.keys($scope.articles).length;

和使用 AngularFire 0.5.0 我可以将它保存:

And using AngularFire 0.5.0 I can save it with:

var name = 'article' + length + 1;
$scope.articles[name] = $scope.article;
$scope.articles.$save(name);

但会发生什么,如果我:

But what happens if I:

$scope.articles.$remove('article2');

和使用相同的方法添加另一条记录? 我们可能会创建重复的键名。的

And add another record using the same approach? We're likely to create duplicate key names.

要加一点复杂,让我们添加一个单一的关系,并说每个文章的有注释

To add a little complexity, let's add a single relationship and say that each article has comments.

什么是该数据在火力地堡集模型的正确方法是什么?

What is the correct way to model this data in a Firebase collection?

推荐答案

请使用 $添加,让火力地堡自动生成按时间顺序排列的列表你。

Please use $add and let Firebase automatically generate chronologically ordered lists for you.

var ref = new Firebase("https://gigablox.firebaseio.com/articles/");
$scope.articles = $firebase(ref);

$scope.addArticle = function() {
  $scope.articles.$add($scope.article);
}

$scope.removeArticle = function(id) {
  $scope.articles.$remove(id);
}

当你调用 $添加

火力地堡自动创建的键名。您可以遍历使用这些按键的名称 NG-重复

<div ng-repeat="(key, article) in articles">
  <div ng-model="article"><a ng-click="removeArticle(key)">Remove</a></div>
</div>