Pergunta

I have a controller which calls getCurrentUser from a service and if a record is returned, adds the necessary additional information to the $scope. If no record is found, this is a new user and I have to get additional information before he/she can continue. I have a getLoggedInUser function to compute all of the variables I can for the new user from SharePoint's user model and then I present a form for the rest.

My question is can I only call getLoggedInUser IF there is no record found or do I have to call both everytime (and put variables not needed in the $scope) so that I have access to it when I need it?

This is how I have it now -- calling the getLoggedInUser first to have the info available if I return no records. It just doesn't seem like this is proper because most of the time I won't need that extra computing since most users will already exists.

I am just learning AngularJS so I appreciate your help.

(function(){

    var MainController = function($scope, SharePointJSOMService){
        $scope.current_user = [];
        $scope.loggedInUser = [];

        SP.SOD.executeOrDelayUntilScriptLoaded(runMyCode, "SP.js");

        function runMyCode(){
            $scope.spReady = _spPageContextInfo;

            $.when(SharePointJSOMService.getLoggedInUser())
                .done(function(jsonObject){
                    $scope.loggedInUser = jsonObject.d;

                    $scope.UserName = $scope.loggedInUser.LoginName;
                    $scope.Display_Name_Full = $scope.loggedInUser.Title;
                    $scope.Display_Name = $scope.Display_Name_Full.substring(0, $scope.Display_Name_Full.indexOf('(')-1);
                    $scope.Email_365 = $scope.loggedInUser.Email;
                    $scope.Email_Company = $scope.UserName.substring($scope.UserName.lastIndexOf('|')+1);
                    $scope.FName = $scope.Email_Company.substring(0, $scope.Email_Company.indexOf('.'));
                    $scope.LName = $scope.Email_Company.substring($scope.Email_Company.indexOf('.')+1, $scope.Email_Company.indexOf('@'));
                    $scope.$apply();
                })
                .fail(function(err){
                    $scope.prefs = true;
                    console.info(JSON.stringify(err)); 
                });

            $.when(SharePointJSOMService.getCurrentUser())
                .done(function(jsonObject){
                    if(jsonObject.d.results.length < 1){
                        // new user
                        $scope.prefs = true; // force preference pane
                        $scope.current_user = {};
                        $scope.projectClose = 'true';
                        $scope.taskClose = 'true';
                        $scope.projectManagerClose = 'true';
                        $scope.$apply();
                    } else {
                        // existing user
                        $scope.prefs = false;
                        $scope.current_user = jsonObject.d.results[0];
                        switch($scope.current_user.User_Role){
                            case 'USR':
                                $scope.project_view = 'all_cs_proj';
                                $scope.projectClose = 'true';
                                break;
                            case 'RSC':
                                $scope.project_view = 'all_it_proj';
                                $scope.projectClose = 'true';
                                break;
                            case 'RQC':
                                $scope.project_view = 'my_rc_proj';
                                $scope.projectClose = 'true';
                                break;
                            case 'PMG':
                                $scope.project_view = 'my_pm_proj';
                                $scope.projectClose = 'true';
                                break;
                            case 'ADM':
                                $scope.project_view = 'my_amm_proj';
                                $scope.projectClose = 'true';
                                break;
                            default:
                                $scope.project_view = 'my_cs_proj';
                                $scope.projectClose = 'false';
                                break;
                        } // end switch
                        $scope.$apply();
                    } // end if
               })
               .fail(function(err){
                $scope.prefs = true;
                console.info(JSON.stringify(err)); 

        });
    }; // end MainController

    MainController.$inject = ['$scope', 'SharePointJSOMService'];

    angular.module('appITI').controller('MainController', MainController);
})();
Foi útil?

Solução

Not quite sure what you are looking for but see if this fits. Just make another service call from inside the callback function.

I got rid of all of 'apply' because I don't think they are needed but you may have to put some back in. HTH.

function(){

var MainController = function($scope, SharePointJSOMService){
    $scope.current_user = [];
    $scope.loggedInUser = [];

    SP.SOD.executeOrDelayUntilScriptLoaded(runMyCode, "SP.js");

    function runMyCode(){
        $scope.spReady = _spPageContextInfo;

        $.when(SharePointJSOMService.getCurrentUser())
            .done(function(jsonObject){
                if(jsonObject.d.results.length < 1){
                    // new user
                    $.when(SharePointJSOMService.getLoggedInUser())
                       .done(function(jsonObject){
                        $scope.loggedInUser = jsonObject.d;

                        $scope.UserName = $scope.loggedInUser.LoginName;
                        $scope.Display_Name_Full = $scope.loggedInUser.Title;
                        $scope.Display_Name = $scope.Display_Name_Full.substring(0, $scope.Display_Name_Full.indexOf('(')-1);
                        $scope.Email_365 = $scope.loggedInUser.Email;
                        $scope.Email_Company =       $scope.UserName.substring($scope.UserName.lastIndexOf('|')+1);
                        $scope.FName = $scope.Email_Company.substring(0, $scope.Email_Company.indexOf('.'));
                        $scope.LName = $scope.Email_Company.substring($scope.Email_Company.indexOf('.')+1, $scope.Email_Company.indexOf('@'));

                        $scope.prefs = true; // force preference pane
                        $scope.current_user = {};
                        $scope.projectClose = 'true';
                        $scope.taskClose = 'true';
                        $scope.projectManagerClose = 'true';
                      })
                      .fail(function(err){
                        $scope.prefs = true;
                        console.info(JSON.stringify(err)); 
                      });       
                } else {
                    // existing user
                    $scope.prefs = false;
                    $scope.current_user = jsonObject.d.results[0];
                    switch($scope.current_user.User_Role){
                        case 'USR':
                            $scope.project_view = 'all_cs_proj';
                            $scope.projectClose = 'true';
                            break;
                        case 'RSC':
                            $scope.project_view = 'all_it_proj';
                            $scope.projectClose = 'true';
                            break;
                        case 'RQC':
                            $scope.project_view = 'my_rc_proj';
                            $scope.projectClose = 'true';
                            break;
                        case 'PMG':
                            $scope.project_view = 'my_pm_proj';
                            $scope.projectClose = 'true';
                            break;
                        case 'ADM':
                            $scope.project_view = 'my_amm_proj';
                            $scope.projectClose = 'true';
                            break;
                        default:
                            $scope.project_view = 'my_cs_proj';
                            $scope.projectClose = 'false';
                            break;
                    } // end switch
                } // end if
           })
           .fail(function(err){
            $scope.prefs = true;
            console.info(JSON.stringify(err)); 

    });
}; // end MainController

MainController.$inject = ['$scope', 'SharePointJSOMService'];

angular.module('appITI').controller('MainController', MainController);
})();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top