firebase - 对一个对象使用一个 ref 并推送到它的子数组上 [英] firebase - Use one ref for an object and push onto it's child array
问题描述
此代码有效,但是我使用两个引用来访问同一对象的不同区域.
This code works however I'm using two refs to access different areas of the same object.
一个例程可能有多个页面.有没有办法创建新页面并只使用一个 ref 将页面推送到 $scope.routine.pages 上,同时也创建一个唯一的 id?
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();
}]);
免责声明:不是我的最终代码,我正在学习 firebase 的工作原理,看看它是否是一个好的解决方案.
Disclaimer: Not my final code I'm learning how firebase works to see if it's a good solution.
推荐答案
我不认为使用多个引用有任何问题.与服务器的连接只有一个.
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'));
这里是文档 我指的是.
这篇关于firebase - 对一个对象使用一个 ref 并推送到它的子数组上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!