我需要從每個控制器的firebase中獲取一組products
,如果我不必每次都進行API調用,那將是理想選擇。用angularfire庫實現這個最好的方法是什麼?在angularjs應用程序中將重複調用放到firebase的位置?
https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray
現在我有(在CoffeeScript中):
app = angular.module 'EApp', ['ui.router', 'firebase']
app.config ($stateProvider, $urlRouterProvider) ->
$stateProvider
.state 'catalog',
url: '/catalog'
templateUrl: 'partials/catalog.html'
controller: 'catalogCtrl'
.state 'product',
url: '/product/:id'
templateUrl: 'partials/product.html'
controller: 'productCtrl'
$urlRouterProvider.otherwise 'catalog'
return
app.controller 'catalogCtrl', ($scope, $rootScope, $firebase) ->
ref = new Firebase("https://my-url.firebaseIO.com/")
$scope.products = $firebase(ref).$asArray()
return
app.controller 'productCtrl', ($scope, $rootScope, $stateParams, $firebase) ->
ref = new Firebase("https://my-url.firebaseIO.com/")
$scope.products = $firebase(ref).$asArray()
return
有沒有辦法在2個控制器以分解出公共代碼,並在$rootScope
(或類似)級別設置products
一旦產品退回,我可以在productCtrl
中做_.find
?
撥打服務,並在該服務中有你的數據變量開始爲空。在第一次firebase調用中,設置你的數據變量,並在每次後續調用時檢查null,如果爲null,則再次從firebase中檢索,否則使用存儲的內容。 – tymeJV 2014-10-03 00:57:51