Firebase AngularFire 隐式和显式同步之间的区别 [英] Difference between Firebase AngularFire implicit and explicit sync

查看:21
本文介绍了Firebase AngularFire 隐式和显式同步之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Firebase 的新手.我想用 angularjs 构建它,我找到了 angularfire.

I am new to firebase. I want to build it with angularjs and I found angularfire.

在 angularFire 文档中列出隐式和显式同步.我试图理解github中的文档,但我仍然不明白它们有什么区别以及如何使用它们.angularFire()angularFireCollection()

In the angularFire docs listed Implicit and explicit sync. I tried to understand the document in github but I still don't understand what is the difference and how to use them. angularFire() and angularFireCollection()

另外,angularFire()angularFireCollection() 中的参数是什么意思?

also, what are the arguments mean in angularFire() and angularFireCollection()?

感谢广告

推荐答案

如果您想要隐式同步,请使用 angularFire,即对模型所做的任何更改都会立即传播到所有其他客户端(反之亦然)).

Use angularFire if you want implicit sync, i.e. any changes made to your model will instantly propagate to all other clients (and vice versa).

如果您想控制必须将任何本地数据更改发送到服务器的时间,请使用 angularFireCollection.任何远程更改仍会自动更新您的本地收藏.

Use angularFireCollection if you want to be in control of when any local data changes must be sent to the server. Any remote changes will still automatically update your local collection.

隐式同步:

myapp.controller('MyCtrl', ['$scope', 'angularFire',
  function MyCtrl($scope, angularFire) {
    var promise = angularFire(url, $scope, 'items', []);
  }
]);

第一个参数是您要存储/检索数据的 Firebase 的位置.第二个参数是范围,第三个参数是 $scope 下的属性名称,您希望在承诺完成后立即绑定数据.例如:

First argument is the location of the Firebase at which you want to store/retrieve data. Second argument is the scope, third argument is the name of the property under $scope you want the data bound as soon as the promise is fulfilled. For example:

promise.then(function() {
  // Data available in $scope.items
});

第四个参数是你想要的 JS 对象中的数据类型.[] 用于数组,{} 用于对象,"" 用于字符串,1 用于数字和 >true 用于布尔值.请注意,如果提供的 Firebase 位置中没有数据,您也可以使用此参数设置默认值.

Fourth argument is the type of data you want in your JS object. Use [] for arrays, {} for objects, "" for strings, 1 for numbers and true for boolean. Note that if no data is present in the provided Firebase location you can also use this argument to set the default value.

在隐式同步中,如果您想进行任何更改,只需修改 $scope.items,更改将通过 Firebase 自动与所有其他客户端同步.同样,远程进行的任何更改都会自动更新 $scope.items.

In implicit sync if you want to make any changes, simply modify $scope.items and the change will automatically synchronize with all other clients via Firebase. Similarly, any changes made remotely will automatically update $scope.items.

显式同步:

myapp.controller('MyCtrl', ['$scope', 'angularFireCollection',
  function MyCtrl($scope, angularFireCollection) {
    $scope.items = angularFireCollection(url);
  }
]);

这个方法只接受一个参数.如果要添加或删除项目,请使用 addremoveupdate 方法.例如:

This method only takes one argument. If you want to add or remove items, use the add, remove or update methods. For example:

$scope.items.add({test: "object"});

由于 angularFireCollection 不需要范围,如果您想在控制器之外使用 Firebase(如角度指令、模块等),您也可以使用它.希望这会有所帮助!

Since angularFireCollection does not require a scope, you can also use this if you want to use Firebase outside of a controller (like angular directives, modules, etc.) Hope this helps!

这篇关于Firebase AngularFire 隐式和显式同步之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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