firebase - 使用一个参考对象,并推到它的子数组 [英] firebase - Use one ref for an object and push onto it's child array
问题描述
这段代码可以工作,但是我使用两个引用来访问同一个对象的不同区域。
例程可能有多个页面。有没有一种方法来创建新的网页,只需使用一个裁判推送页面到$ scope.routine.pages,也创建一个唯一的ID?
角.module('screenTester.auth.routine',[
'screenTester.auth.routine.page'
])
.config(['$ stateProvider',function $ state $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$' ',
templateUrl:'/dist/auth/routine/routine.html'
});
}])
.controller(' RoutineCtrl',['$ scope','$ stateParams','$ firebase',函数($ scope,$ stateParams,$ firebase){
$ scope.routineId = $ stateParams.routineId;
//结合这两者,仍然创建具有唯一ID的页面?
var ref = new Fire base(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 +'/页面);
var pagesSync = $ firebase(ref);
$ scope.createPage = function(){
//我会在$ 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是如何工作的,看看它是否是一个好的解决方案。
我不认为以任何方式使用多个引用是一个问题。连接到服务器只有一个。
然而,如果你想重用这个实例,你可以尝试这样做:
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'));
这里是 documentation 我指的是
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.
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.
这篇关于firebase - 使用一个参考对象,并推到它的子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!