如何使用AngularFire自动绑定优化数据检索和控制器间数据共享? [英] How to optimise data retrieval and inter-controller data sharing with AngularFire automatic binding?
问题描述
- 列表视图+控制器,显示项目名称列表
<李>详细视图+控制器多数民众赞成在一个项目的细节。
如何使用angularfire(最好使用自动三向绑定)以不同次获取相同的数据?
因为当(itemsListRef)。$ bind($ scope,'items')时,控制器#1已经获取了项目及其细节。 ;执行
语句。在控制器#2中,(itemDetailRef,)。$ bind($ scope,'itemDetail')
然后将获取已经在控制器#1中获取的项目细节数据。
一个可以想到的选项:一个角度服务可以用来获取项目数据一次,然后在控制器之间共享数据,但我不知道如何angularfire三自动绑定会发挥到这一点,或者如果它甚至工作。你的想法/建议呢?任何其他选项?什么是推荐的方式来处理这个?
Firebase客户端足够聪明,可以使用本地缓存(如果存在)。 Firebase实例对于每个基本Firebase网址都是单身,因此在同一Firebase中的不同路径中添加多个侦听器将导致不必要的网络活动。
我建议在服务中创建一个 $ firebase
引用,然后使用3-way数据绑定来获取控制器中的项目的详细信息。例如:
$ b
var myapp = angular.module(myapp,[firebase] );
$ b myapp.factory(ItemService,[$ firebase,function($ firebase){
return $ firebase(itemsListRef);
}]);
$ b myapp.controller(DetailCtrl,[$ scope,ItemService,function($ scope,items){
items。$ child(itemId)。$ bind($范围,'itemDetail');
}]);
Say I have
- a list view+controller that displays a list of item names and
- a detail view+controller thats shows detail for an item.
How would I use angularfire (preferably with automatic three way binding) in a way that does not fetch the same data twice?
Because controller#1 would have already fetched the items AND their details when the (itemsListRef).$bind($scope, 'items');
statement executes. In controller#2, (itemDetailRef,).$bind($scope, 'itemDetail')
would then fetch item detail data that again that has already been fetched in controller#1.
One option that comes to mind : An angular service could be used to fetch the items data once and then share the data between controllers, but I am not sure how angularfire three-way automatic binding would play into this, or if it would even work. Your thoughts/advise on this? Any other options? What would be the recommended way to handle this?
The Firebase client is smart enough to use a local cache if it exists. Firebase instances are singletons for each base Firebase URL, so attaching multiple listeners at varying paths in the same Firebase will not result in unnecessary network activity.
I recommend creating a $firebase
reference in a service, and then using a 3-way data bind for the details of the item in a controller. For example:
var myapp = angular.module("myapp", ["firebase"]);
myapp.factory("ItemService", ["$firebase", function($firebase) {
return $firebase(itemsListRef);
}]);
myapp.controller("DetailCtrl", ["$scope", "ItemService", function($scope, items) {
items.$child(itemId).$bind($scope, 'itemDetail');
}]);
这篇关于如何使用AngularFire自动绑定优化数据检索和控制器间数据共享?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!