我最近发现了David East's firebase blog post并且已经开始将我的$ loaded使用量从我的控制器移动到每个视图的解析。出现的一个问题涉及我使用firebaseRef检索$ firebaseArray的情况,该firebaseRef包含来自另一个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
提前致谢!
答案 0 :(得分:0)
所以我发现了一些有效的东西,但我不确定它是否是最好的做法。通过单独执行每个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
如果有更好/更推荐的方法,我仍然非常感谢听到它。