Brent Rusinow
Brent Rusinow

Reputation: 49

When resolve returning $firebaseArray needs return of $firebaseObject service first?

I recently discovered David East's firebase blog post and have started to move my $loaded usage from my controllers to my resolves for each view. One issue that has come up involves a situation where I'm retrieving a $firebaseArray with a firebaseRef which contains something from another firebase database call. My ref for the second part would be something like this:

var chatRef = firebase.database().ref('messages/'+thisGroup.groupId+'/main');

Is this a situation where I can only unwrap one promise through the resolve, or am I close doing something like this (ActiveGroup is a service which returns a $firebaseObject of the user's currently selected group):

.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();
        })    
      }
    }
})

And then here is where I am injecting posts in the controller:

    .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

Thanks in advance!

Upvotes: 4

Views: 282

Answers (1)

Brent Rusinow
Brent Rusinow

Reputation: 49

So I found something that works but I'm not sure if it's the best practice. By doing each firebase call separately (and in the proper order), this seems to do the trick:

.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();    
    }       
}
})

And then the controller where you inject it:

    .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

If there is a better/more recommended way of doing this, I'd still really appreciate hearing it.

Upvotes: 0

Related Questions