我最近发现了David East的firebase博客文章,并开始将我的$loaded用法从控制器转移到每个视图的解析中。出现的一个问题涉及这样一种情况,即我正在检索带有firebaseRef的$firebaseArray,其中包含来自另一个firebase数据库调用的内容。我对第二部分的参考是这样的:
var chatRef = firebase.database().ref('messages/'+thisGroup.groupId+'/main');
这是一种我只能通过解析打开一个承诺的情况,还是我即将做这样的事情(ActiveGroup是一种返回用户当前选择的组的$firebaseObject的服务):
.state('tab.chats-main', {
url: '/chats/main',
cache: true,
views: {
'tab-chats': {
templateUrl: 'templates/tab-chatsTopic.html',
controller: 'ChatsTopicCtrl'
}
},
resolve: {
currentAuth: authRequire,
posts: function($firebaseArray, currentAuth, ActiveGroup){
var thisGroup = ActiveGroup(currentAuth.uid);
thisGroup.$loaded().then(function(){
var chatRef = firebase.database().ref('messages/'+thisGroup.groupId+'/main');
return $firebaseArray(chatRef.limitToLast(100)).$loaded();
})
}
}
})
然后这里是我在控制器中注入帖子的地方:
.controller('ChatsTopicCtrl', function($scope, thisGroup, posts, profile, chatName, chatMessages, currentAuth, ActiveGroup, $cordovaCamera, $ionicScrollDelegate, $ionicModal, $ionicActionSheet, $timeout,$state, moment) {
console.log("what are posts? ",posts); // posts is undefined
提前感谢!
所以我找到了一些有效的方法,但我不确定这是否是最佳实践。通过分别(按照正确的顺序)进行每个firebase调用,这似乎达到了目的:
.state('tab.chats-main', {
url: '/chats/main',
cache: true,
views: {
'tab-chats': {
templateUrl: 'templates/tab-chatsTopic.html',
controller: 'ChatsTopicCtrl'
}
},
resolve: {
currentAuth: authRequire,
thisGroup: function(ActiveGroup, currentAuth){
return ActiveGroup(currentAuth.uid).$loaded();
},
posts: function($firebaseArray, thisGroup){
var chatRef = firebase.database().ref('messages/'+thisGroup.groupId+'/main');
return $firebaseArray(chatRef.limitToLast(100)).$loaded();
}
}
})
然后是你注入它的控制器:
.controller('ChatsTopicCtrl', function($scope, thisGroup, posts, profile, chatName, chatMessages, currentAuth, ActiveGroup, $cordovaCamera, $ionicScrollDelegate, $ionicModal, $ionicActionSheet, $timeout,$state, moment) {
console.log("what is posts? ",posts); // gives proper result
如果有更好/更推荐的方法,我仍然非常感谢听到它。