我有一个控制器从服务调用 getCurrentUser ,如果返回一个记录,则向$ scope添加必要的附加信息。如果没有找到记录,这是一个新用户,我必须在他/她可以继续之前获得更多信息。我有一个 getLoggedInUser 函数,可以从SharePoint的用户模型中为新用户计算所有变量,然后为其余部分提供一个表单。
我的问题是我可以只调用getLoggedInUser IF 没有找到记录,或者每次都要调用它们(并且在$ scope中放置不需要的变量)以便我可以访问它什么时候需要它?
这就是我现在的方法 - 如果我没有返回任何记录,首先调用getLoggedInUser来获取信息。它似乎并不合适,因为大部分时间我都不需要额外的计算,因为大多数用户已经存在。
我正在学习AngularJS,所以感谢您的帮助。
(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);
})();
答案 0 :(得分:1)
不太确定你在寻找什么,但看看它是否合适。只需从回调函数内部进行另一个服务调用。
我摆脱了所有'申请'因为我认为不需要它们但你可能不得不重新投入.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);
})();