如何使用 AngularFire 自动绑定优化数据检索和控制器间数据共享? [英] How to optimise data retrieval and inter-controller data sharing with AngularFire automatic binding?

查看:22
本文介绍了如何使用 AngularFire 自动绑定优化数据检索和控制器间数据共享?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有

  1. 一个列表视图+控制器,显示项目名称和
  2. 显示项目详细信息的详细视图+控制器.

我将如何使用 angularfire(最好使用自动三向绑定)以不两次获取相同数据的方式使用?

How would I use angularfire (preferably with automatic three way binding) in a way that does not fetch the same data twice?

因为当 (itemsListRef).$bind($scope, 'items'); 语句执行时,控制器#1 已经获取了项目及其详细信息.在控制器#2 中,(itemDetailRef,).$bind($scope, 'itemDetail') 然后将再次获取已经在控制器#1 中获取的项目详细信息数据.

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.

想到的一个选项:角度服务可用于获取项目数据一次,然后在控制器之间共享数据,但我不确定 angularfire 三向自动绑定将如何发挥作用,或者是否可以甚至会工作.您对此有何想法/建议?还有其他选择吗?处理此问题的推荐方法是什么?

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?

推荐答案

Firebase 客户端足够智能,可以使用本地缓存(如果存在).Firebase 实例是每个基本 Firebase URL 的单例,因此在同一个 Firebase 的不同路径上附加多个侦听器不会导致不必要的网络活动.

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.

我建议在服务中创建一个 $firebase 引用,然后使用三向数据绑定来获取控制器中项目的详细信息.例如:

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屋!

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