如何使用resolve将我的firebase-data返回到$ scope

时间:2014-08-28 14:42:28

标签: angularjs firebase

我用来从firebase获取数据的服务:

'use strict';

angular.module('flbi.services.trainings', [])
    .factory('trainingsService', ['FBURL',
        function(FBURL) {
            return {
                getList: function() {

                    var queryLimit = 10;
                    var firebase = new Firebase(FBURL);

                    firebase.child('trainings').limit(queryLimit).on('value', function(trainings) {

                        var allTrainings = trainings.val();

                        $.each(allTrainings, function(training) {

                            firebase.child('users/' + allTrainings[training].userid).on('value', function(user) {

                                allTrainings[training].user = user.val();
                                allTrainings[training].user.gravatar = MD5(allTrainings[training].user.email);
                            });

                        });
                    });
                }
            };
        }]);

函数getList()从:

调用
$routeProvider
            .when('/', {
                controller: 'trainingsCtrl',
                templateUrl: 'views/default.html',
                resolve: {
                    "trainings": function(trainingsService) {
                        return trainingsService.getList();
                    }
                }
            })

控制器:

'use strict';

angular.module('flbi.controllers.trainings', ['flbi.services.trainings'])
    .controller('trainingsCtrl', ['$scope', 'trainings',
        function($scope, trainings) {

            console.log(trainings); <-- just empty ....
            $scope.trainings = trainings;
        }]);

如何将allTraining的数据返回给我的控制器?我总是得到一个空的对象。但是,如果我检查console.log(allTrainings)内部的on() - 服务方法,它充满了数据......

1 个答案:

答案 0 :(得分:1)

您解决方法必须返回一个promise才能使其按预期工作。所以你的getList方法应该返回一个promise。

此外,首选snapshot.forEach()使用.val(),因为它是高度优化的(它迭代指针而不是解析并将所有数据收集到一个对象中,它还对记录进行排序以匹配数据,因为JavaScript对象本质上是无序的。)

angular.module('flbi.services.trainings', [])
.factory('trainingsService', ['FBURL', '$q',
  function(FBURL, $q) {
    return {
      getList: function() {
        var def = $q.defer();

        var queryLimit = 10;
        var firebase = new Firebase(FBURL);

        firebase.child('trainings').limit(queryLimit).on('value', function(trainings) {
          var promises = [];
          var allTrainings = {};
          trainings.forEach(function(ss) {
            var key = ss.name();
            var d = $q.defer();
            promises.push(d.promise);
            // put any other data you need in the trainings keys here
            // allTrainings[key].widget = ss.child('widget').val();
            firebase.child('users/' + allTrainings[key].userid).on('value', function(user) {

              allTrainings[key].user = user.val();
              var email = user.child('email').val();
              allTrainings[key].user.gravatar = MD5(email);
              d.resolve();
            }, d.reject);
            $q.when(promises).then(function() {
              def.resolve(allTrainings);
            }, def.reject);
          });
        }, def.reject);

      return def.promise;
      }
    };
  }
]);