Using recordWatch and $rootscope timing issue

davilu
Mega Sage

Our team has a recordWatch on a widget that works great, but we also want to use rootscope to broadcast data to another widget and have something else update as well.  The issue we're running into is a delay in the updating and we're not sure how to mitigate it.  

Our Server Script looks like this:

	data.tasksCompleted = false;

	if (input && input.action === 'getTasks')
		getTasks(data.user_id);

	function getTasks(userId) {
		data.tasks = new sn_hr_core.hr_Task().getMyTasks(data.user_id);
		for (var i = data.tasks.length-1; i >= 0; i--) {
			var task = new sn_hr_sp.hr_TaskTicket().getTasks(data.tasks[i].sys_id, 'sn_hr_core_task');
			data.tasks[i].taskInfo = task;
			if(data.tasks[i].state == '3'){
				data.tasksCompleted=true;
			}
		}
	}

And here's our Client Script with the recordWatch:

	spUtil.recordWatch($scope, 'sn_hr_core_task', '',function(event,data){
		spUtil.update($scope);
		setTimeout(function(){ 
			$rootScope.$broadcast('onboardingCompletedTasks', $scope.data.tasksCompleted);  
		}, 5000);	
	});

We had to use a setTimeout with a 5 second delay in order for the other widget to receive the tasksCompleted flag and update accordingly.  Without the 5 second delay, the recordWatch works too fast and the other widget does not update at all.  Is there a way to make these things smoother?  Our current way works...but the user experience is terrible.

Any suggestions welcomed.  Thanks!

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

 

spUtil has a .then: 

spUtil.update($scope).then(function(){
	$rootScope.$broadcast('onboardingCompletedTasks', $scope.data.tasksCompleted);
});

Or you can have a trigger outside of the recordwatcher for the broadcast:

spUtil.recordWatch($scope, 'sn_hr_core_task', '',function(event,data){
	spUtil.update($scope);
});


$scope.$watch(passValueWhenChanged, executeBroadcastWhenChanged);
	
function passValueWhenChanged(){
	return $scope.data.tasksCompleted;
}
	
function executeBroadcastWhenChanged(value){
	$rootScope.$broadcast('onboardingCompletedTasks', value); 
}

 

View solution in original post

5 REPLIES 5

ChrisBurks
Mega Sage

 

spUtil has a .then: 

spUtil.update($scope).then(function(){
	$rootScope.$broadcast('onboardingCompletedTasks', $scope.data.tasksCompleted);
});

Or you can have a trigger outside of the recordwatcher for the broadcast:

spUtil.recordWatch($scope, 'sn_hr_core_task', '',function(event,data){
	spUtil.update($scope);
});


$scope.$watch(passValueWhenChanged, executeBroadcastWhenChanged);
	
function passValueWhenChanged(){
	return $scope.data.tasksCompleted;
}
	
function executeBroadcastWhenChanged(value){
	$rootScope.$broadcast('onboardingCompletedTasks', value); 
}

 

awesome, thanks ChrisB!!

Hi ChrisB, I actually have a follow-up question regarding recordWatch.  Our team has a large widget that embeds a bunch of smaller widgets, each one in its own tab.  At first, we tried to put all of our recordWatchers in the big widget, however for whatever reason, that didn't seem to update things dynamically.  We then put the recordWatchers in each of the individual embedded widgets.  This seems to work, but only if the user is already on the correct tab.  For instance, we have a tab for "Work History" and a recordWatch that gives us the count of Work History entries.  If the user is already on the Work History tab, the recordWatch works perfectly and the count goes up as we add more entries to that table.  However, if a user is on another tab, then that recordWatch counter doesn't update until we refresh the page.  

Have you encountered this before?  Would love to hear your thoughts on this.  Thanks!

I have not encountered that before. But I think it would depend on the tab setup. Are the tabs set to render only when the correlating tab is clicked or is each individual tab loaded at once and only hiding until the correlating tab is clicked?