无法在控制器中使用AngularFire从$ firebaseObject获取数据。查看/ ng-repeat工作正常

时间:2015-06-04 20:38:09

标签: angularjs firebase angularfire

我在Firebase中有不同的部分使用规范化数据,我有例程来获取信息,但我无法遍历返回的记录来获取数据。我想使用$ firebaseArray()中的键从其他$ firebaseObject()中获取数据。

GetOneTeam() .... {
    var DataRef = GetFireBaseObject.DataURL(Node + '/');  // xxx.firebaseio.com/Schedules/
    var OneRecordRef = DataRef.child(Key); // Schedule Key - 1
    return $firebaseObject(OneRecordRef);
}

...

var Sched = GetOneSchedule('Schedules', 1);
...   // For Loop getting data - Put in HomeId
var TeamRec = GetOneTeam('Teams', HomeId);
var Name = TeamRec.TeamName;    // Does not TeamName value from Schedule/1

如果上面的代码段不够清晰,以下是更多实际代码。获取数据的常用例程示例:

angular.module('MyApp')
    .constant('FIREBASE_URL', 'https://xxxxxxxx.firebaseio.com/');

angular.module('MyApp')
    .factory('GetFireBaseObject', function(FIREBASE_URL) {
        return {
            BaseURL: function() {
                return new Firebase(FIREBASE_URL);
            },
            DataURL: function(Node) {
                return new Firebase(FIREBASE_URL + Node);
            }
        };
    }
);

// Common code for getting Array/Object from Firebase.  
angular.module('MyApp')
    .factory("FireBaseData", ["$firebaseArray", "$firebaseObject", "GetFireBaseObject", 
    function($firebaseArray, $firebaseObject, GetFireBaseObject) {
        return {
            AllRecords: function(Node) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                return $firebaseArray(DataRef);
            },
            OneRecordAllChildren: function(Node, Key) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                var ParentRecordRef = DataRef.child(Key);
                return $firebaseArray(ParentRecordRef);
            },
            OneRecord: function(Node, Key) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                var OneRecordRef = DataRef.child(Key);
                return $firebaseObject(OneRecordRef);
            },
            AddRecord: function(Node, Record) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                var AddRecordRef = DataRef.child(Record.Key);               
                AddRecordRef.update(Record);
                return $firebaseObject(AddRecordRef);           // Return Reference to added Record
            },
            DeleteRecord: function(Node, Key) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                var DeleteRecordRef = DataRef.child(Key);
                DeleteRecordRef.remove();
            }
        };
    }
]);

Individual Controller从firebase.io中检索记录:

angular.module('MyApp').service("ScheduleData", ["FireBaseData",
    function(FireBaseData) {
        var DataPath = 'Schedules';

        this.AllSchedules = function() {
            return FireBaseData.AllRecords(DataPath);
        };

        this.AddSchedule = function(GameInfo) {
            return FireBaseData.AddRecord(DataPath, GameInfo);
        };

        this.DeleteSchedule = function(GameKey) {
            FireBaseData.DeleteRecord(DataPath, GameKey);
        };      

        this.GetOneSchedule = function(GameKey) {
            return FireBaseData.OneRecord(DataPath, GameKey);
        };
    }
]);

Sample Firebase Record

// Structure of a record, including named fields to come from another object (Team/Venue using the OneRecord FireBaseData call to get a $firebaseObject
angular.module('MyApp').factory("ScheduleRecord", function() {
        return {
            Clear: function(GameInfo) {
                GameInfo.Key = "";
                GameInfo.HomeTeamId = "";
                GameInfo.HomeTeamName = "";
                GameInfo.AwayTeamId = "";
                GameInfo.AwayTeamName = "";
                GameInfo.VenueId = "";
                GameInfo.VenueName = "";
                GameInfo.GameDate = "";
                GameInfo.GameTime = "";
            }
        };
    }
);

控制器模块启动:

angular.module('MyApp').controller('ScheduleCtrl', ["$scope", "ScheduleData", "ScheduleRecord", "TeamData", "VenueData",
    function ($scope, ScheduleData, ScheduleRecord, TeamData, VenueData) {

        var ClearEditData = function() {
            $scope.ScheduleEditMode = false;
            ScheduleRecord.Clear($scope.schedule);
        };

        var GameSchedules = ScheduleData.AllSchedules();

下一篇文章是我的问题所在。一旦承诺返回静态计划列表,我想循环遍历每个记录并将团队ID(Home / Away)和Venue Id翻译成名称。

        GameSchedules.$loaded().then(function() {
            angular.forEach(GameSchedules, function(GameInfo) {
                var HomeTeam = TeamData.GetOneTeam(GameInfo.HomeTeamId);
                GameInfo.HomeTeamName = HomeTeam.Name;

GetOneTeam根据HomeTeamId子记录返回$ firebaseObject。这始终返回null。

这也是使用FireBaseData返回的TeamData.GetOneTeam。

angular.module('MyApp').service("TeamData", ["FireBaseData",
    function(FireBaseData) {
        var DataPath = 'Teams';

        this.AllTeams = function() {
            return FireBaseData.AllRecords(DataPath);
        };

        this.AddTeam = function(TeamInfo) {
            return FireBaseData.AddRecord(DataPath, TeamInfo);
        };

        this.DeleteTeam = function(TeamKey) {
            FireBaseData.DeleteRecord(DataPath, TeamKey);
        };      

        this.GetOneTeam = function(TeamKey) {
            return FireBaseData.OneRecord(DataPath, TeamKey);
        };
    }
]);

由于我有Firebase对象,如何从$ firebaseObject获取我的命名数据对象?

1 个答案:

答案 0 :(得分:9)

这是一团糟。使用$ firebaseArray表示集合,而不是$ firebaseObject。大多数这些奇怪的包装工厂都是不必要的。 AngularFire服务已经有添加,删除等方法,所有这些工厂都试图将AngularFire变成CRUD模型,并且实际上并没有提供任何额外的功能或增强功能。

app.factory('Ref', function(FIREBASE_URL) {
   return new Firebase(FIREBASE_URL);
});

app.factory('Schedules', function($firebaseArray, Ref) {
   return $firebaseArray(Ref.child('Schedules'));
});

// or if you want to pass in the path to the data...
//app.factory('Schedules', function($firebaseArray, Ref) {
//   return function(pathToData) {
//      return $firebaseArray(Ref.child(pathToData));
//   };
//});

app.factory('Schedule', function($firebaseObject, Ref) {
  return function(scheduleId) {
     return $firebaseObject(Ref.child('Schedules').child(scheduleId));
  }
});

app.controller('...', function(Schedules, Schedule, Ref) {

  $scope.newSchedule(data) {
    Schedules.$add(data);
  };

  $scope.removeSchedule(key) {
    Schedules.$remove(key);
  };

  $scope.updateSchedule(key, newWidgetValue) {
     var rec = Schedules.$getRecord(key);
     rec.widgetValue = newWidgetValue;
     Schedules.$save(rec);
  };

  // get one schedule
  var sched = Schedule(key);
  sched.$loaded(function() {
     sched.widgetValue = 123;
     sched.$save();
  });


});