如何运行Firebase和Angularjs Promises,例如$ loaded(),然后从依赖函数中获取值

时间:2014-09-04 08:11:11

标签: angularjs firebase javascript

我正在构建firebase angular app:

var myApp = angular.module('appStart',[]);

myApp.controller('MyController', function($scope,$firebase,,MyFactory){

//this is returning undefined as this point
//How can i pass angualrjs promises so that it returns values once factory finish runnig all operations
    var returnData = MyFactory.getUsersProfile(userId);

});

myApp.factory('MyFactory', ['$firebase', function($firebase){

        var factory = {};

        factory.getUsersProfile = function(userId){

        var firebaseRef = new Firebase("https://xxx.firebaseio.com/userprofile").child(userId);
                //retrive the data
                firebaseRef.on('value', function (snapshot) {
                    console.log(snapshot.val());
                    var data = snapshot.val();
                    if (data){
 //Here I want to call another function and get and return values once called function finish running
                        var data = factory.getUsersBetsEventsDetails(data); 
                        return data;
                    }else{
                        console.log('return nothing');
                    }
                }, function (errorObject) {
                    console.log('The read failed: ' + errorObject.code);
                });
            }

            factory.getUsersBetsEventsDetails = function(usersProfileData){
                var firebaseBetsRef = new Firebase("https://xxx.firebaseio.com/e");
                var userE = [];
                angular.forEach(usersProfileData, function(usersProfileData){
                    console.log('getUsersBets',usersProfileData);
                    var firebaseEData = firebaseBetsRef.child(usersProfileData.e_id);
                    //retrive the data 
                    firebaseE.on('value', function (snapshot) {
                        console.log(snapshot.val());
                        userE.push(snapshot.val());
                        console.log('userE',userE);
                    }, function (errorObject) {
                        console.log('The read failed: ' + errorObject.code);
                    });
                    //this is the values controller should get 
                    retuen userE;
                }); 
            }

            return factory;
        }]);

正如我在评论中写的那样,我希望在完成运行后运行相关代码并且不确定如何实现这一点?

此代码中您不确定的是什么?请问。感谢