This is a migrated thread and some comments may be shown as answers.

Using JS Promises in moveEnd

2 Answers 172 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Grant
Top achievements
Rank 3
Iron
Iron
Veteran
Grant asked on 13 Aug 2019, 02:05 PM

Hi, 

How can I get the moveEnd to wait until a promise is resolved before ending completing?

Currently I have the following:

moveEnd: function(e) {
  if (!isEventValid(e.start, e.end)) {
    e.preventDefault();
    return false;
  }
 
  $.when(getAssistance(e.event, e.start, e.end))
    .done(function() {
      window.location = "/new-url";
    }).
    fail(function() {
      // 1
      e.preventDefault();
      return false;
    });
}

 

(I dont know how to get out of this editor)

My problem is that the 'moveEnd' completes before the promise is complete. If the Promise resolved and the user redirected, its a non-issue, but if the promise is rejected, the move needs to be prevented.

Please advise, Thanks,
Grant

2 Answers, 1 is accepted

Sort by
0
Accepted
Veselin Tsvetanov
Telerik team
answered on 15 Aug 2019, 07:38 AM
Hi Grant,

I am afraid that you won't be able to wait until the promise has been resolved to determine whether the moveEnd event should be prevented or not. That is why I would suggest you a different approach. If the promise fails, instead of using the preventDefault() call you could directly re-set the event start and end dates. To do that you should handle the moveEnd event in the following way:
moveEnd: function(e) {
  var initialStart = e.event.start;
  var initialEnd = e.event.end;
  var event = e.event;
 
  $.when(getAssistance(e.event, e.start, e.end))
    .done(function() {
    window.location = "/new-url";
  }).
  fail(function() {
    // 1
    event.set('start', initialStart);
    event.set('end', initialEnd);
    return false;
  });
}
 
Regards,
Veselin Tsvetanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Grant
Top achievements
Rank 3
Iron
Iron
Veteran
answered on 16 Aug 2019, 06:40 AM

Thanks Veselin,

Worked great.

Regards,
Grant

Tags
Scheduler
Asked by
Grant
Top achievements
Rank 3
Iron
Iron
Veteran
Answers by
Veselin Tsvetanov
Telerik team
Grant
Top achievements
Rank 3
Iron
Iron
Veteran
Share this question
or