[英]ServiceNow: issue with recordWatch and $rootscope timing
Our team is developing an application in ServiceNow. 我们的团队正在ServiceNow中开发应用程序。 We have a recordWatch on a widget that updates dynamically, but we also want to use rootscope to broadcast data to another widget and have something else update as well.
我们在一个可动态更新的窗口小部件上有一个recordWatch,但我们也想使用rootscope将数据广播到另一个窗口小部件,并且还要进行其他更新。 The issue we're running into is a delay in the $rootscope and we're not sure how to mitigate it.
我们遇到的问题是$ rootscope的延迟,我们不确定如何缓解它。
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 our Client Script with the recordWatch looks like this: 我们的带有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. 为了使其他小部件能够接收taskCompleted标志并进行相应的更新,我们必须使用具有5秒延迟的setTimeout。 Without the 5 second delay, the recordWatch works too fast and the other widget does not update at all.
如果没有5秒的延迟,recordWatch的工作速度将太快,而其他小部件则根本不会更新。 Is there a way to make these things smoother?
有没有办法使这些事情变得更顺畅? Our current way works...but the user experience is terrible.
我们当前的方式可行...但是用户体验很糟糕。
Any suggestions welcomed. 任何建议欢迎。 Thanks!
谢谢!
The root problem here is that spUtil.update($scope);
这里的根本问题是
spUtil.update($scope);
is async and the $broadcast will happen before the Server has loaded all Tasks. 是异步的,并且$ broadcast将在服务器加载所有任务之前发生。
What you need to do is to send the $broadcast after your server logic is done. 您需要做的是在服务器逻辑完成后发送$ broadcast。
I would suggest you watch data.tasksCompleted on the client and put the broadcast in the callback: 我建议您在客户端上观看data.tasksCompleted并将广播内容放入回调中:
$scope.$watch(data.tasksCompleted, , function(value) {
if (value)
$rootScope.$broadcast('onboardingCompletedTasks', $scope.data.tasksCompleted);
});
This way, when data.tasksCompleted updates from the server to true the broadcast will be sent. 这样,当data.tasksCompleted从服务器更新为true时,将发送广播。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.