Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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
Giga 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

hmm that is a very good question...we're currently getting this to function by using ng-switch.  Do you know if this loads everything at once or only when clicked?  If it's the latter, do you have any suggestions on a different method?

Now that you bring that up, I'm pretty sure it's only loading when the corresponding tab is clicked...which would explain all of these $rootScope/recordWatch issues we're running into.

Thanks for your help!