GO VEGAN
GO VEGAN

Reputation: 1143

firebase with angular router never reaches controller

I want to make sure I get the data from firebase before my app is loaded. I am using this code but it's not working: the program never reach the code deferred.resolve(); and the appCtrl does not run... Any idea?

appCtrl :

.controller('AppCtrl', function($scope, $ionicModal, $timeout,$firebaseAuth,$state,myWordsListServ) {    
  $scope.myWordsList = myWordsListServ.contacts;
  console.log("myWordsList" , $scope.myWordsList);
})

myWordsListServ:

.factory('myWordsListServ', function($firebase, $q) {
  return {
    contacts: null,
    promiseToHaveMyLists: function() {
      var deferred = $q.defer();

       var firebaseMainRef = new Firebase("https://myApp.firebaseio.com");
       var authData = firebaseMainRef.getAuth();
       if(authData){
         this.contacts = $firebase(new Firebase("https://myApp.firebaseio.com/users/" + authData.uid));
         this.contacts.$on('value', function(loadedData) {
           deferred.resolve();
         });
       }
       else{
         deferred.resolve();
       }
      return deferred.promise;
    }
  };
})

part of my app.js:

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "templates/menu.html",
    controller: 'AppCtrl',
    resolve : {
        myWordsList : function(myWordsListServ) {
            return myWordsListServ.promiseToHaveMyLists();
        }
    },
  })

Upvotes: 0

Views: 57

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599521

You seem to be using a pre-1.0 version of AngularFire:

this.contacts = $firebase(new Firebase("https://myApp.firebaseio.com/users/" + authData.uid));
this.contacts.$on('value', function(loadedData) {
  deferred.resolve();
});

The $firebase class was an intermediate object and (if I recall correctly) would never find events. What you're probably looking to do is:

this.contacts = $firebase(new Firebase(...)).$asObject();

This returns a $firebaseObject which does fire events.

You might want to upgrade to AngularFire 1.0, in which case you'd get the same result with:

this.contacts = $firebaseObject(new Firebase(...));

Upvotes: 1

Related Questions