问题
This code works however I'm using two refs to access different areas of the same object.
A routine may have multiple pages. Is there a way to create new pages and just use one ref to push pages onto $scope.routine.pages that also create a unique id?

angular.module('screenTester.auth.routine', [
'screenTester.auth.routine.page'
])
.config(['$stateProvider', function($stateProvider) {
$stateProvider
.state('auth.routine', {
url: '/routine/:routineId',
controller: 'RoutineCtrl',
templateUrl: '/dist/auth/routine/routine.html'
});
}])
.controller('RoutineCtrl', ['$scope', '$stateParams', '$firebase', function($scope, $stateParams, $firebase) {
$scope.routineId = $stateParams.routineId;
// Combine these two and still create pages with a unique id?
var ref = new Firebase("https://url.firebaseio.com/user" + $scope.auth.user.id + '/routines/' + $scope.routineId);
var sync = $firebase(ref);
var ref = new Firebase("https://url.firebaseio.com/user" + $scope.auth.user.id + '/routines/' + $scope.routineId + '/pages');
var pagesSync = $firebase(ref);
$scope.createPage = function() {
// What method would I use here on `$scope.routine`?
$scope.routinePages.$add($scope.newPage);
$scope.routinePages.$save();
$scope.newPage = {};
$scope.toggles.showCreatePage = false;
};
$scope.init = function() {
$scope.routine = sync.$asObject();
$scope.routinePages = pagesSync.$asArray();
$scope.newPage = {};
$scope.toggles = {};
};
$scope.init();
}]);
Disclaimer: Not my final code I'm learning how firebase works to see if it's a good solution.
回答1:
I don't think using multiple references is a problem in any way. The connection to server is only one.
however if you want to reuse the instance you can try doing something like this:
var ref = new Firebase("https://url.firebaseio.com/user" + $scope.auth.user.id + '/routines/' + $scope.routineId);
var sync = $firebase(ref);
var pagesSync = $firebase(ref.child('pages'));
and here is the documentation I refer to.
来源:https://stackoverflow.com/questions/25105938/firebase-use-one-ref-for-an-object-and-push-onto-its-child-array