Using JS Promises in moveEnd

3 posts, 1 answers
  1. Grant
    Grant avatar
    163 posts
    Member since:
    Jul 2016

    Posted 13 Aug 2019 Link to this post

    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. Answer
    Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1201 posts

    Posted 15 Aug 2019 Link to this post

    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.
  3. Grant
    Grant avatar
    163 posts
    Member since:
    Jul 2016

    Posted 16 Aug 2019 in reply to Veselin Tsvetanov Link to this post

    Thanks Veselin,

    Worked great.

    Regards,
    Grant

Back to Top