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

Execute PreventDefault in Deferred callback

1 Answer 135 Views
Sortable
This is a migrated thread and some comments may be shown as answers.
Grant
Top achievements
Rank 2
Veteran
Iron
Grant asked on 20 Feb 2017, 10:11 AM

Hi, 

Are there any limitations when executing 'preventDefault()' in the end Callback of the Sortable widget? Below is a snippet of my 'end' callback that Im having trouble with. 

end: function(e) {
    if (e.oldIndex == e.newIndex) {
        e.preventDefault();
        return false;
    }
 
    $.when(promptForDuration())
        .done(function(result) {
                alert("success: " + result)
        })
        .fail(function() {
                console.log(e);
            e.preventDefault();
            return false;
        });
}

If the old and new index are the same, the 'preventDefault()' works fine, rolling back any chages made by the user. However if they are not the same I've created a custom KendoWindow to prompt for a new duration to be entered and returned using deferred promises (not sure if I've described that right).
The 'fail' callback is triggering and the console.log is printing the sortable event info:

Object {
  _defaultPrevented: true,
  action: "sort",
  draggableEvent: Object,
  isDefaultPrevented: (),
  item: A.fn.init[1],
  newIndex: 1,
  oldIndex: 0,
  preventDefault: (),
  sender: init,
  __proto__: Object }

But still the 'preventDefault()' is not either not running or not having an affect of the Sortable widget.

Please advise on how this can be resolved.

Kind Regards,
Grant

1 Answer, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 22 Feb 2017, 09:49 AM
Hello Grant,

Based on the provided information and inspecting the scenario this is a timing issue. When the fail callback is called it is too late to call the preventDefault method.

Please observe this in the following Dojo(I used the done callback, but the result with the fail is similar):

http://dojo.telerik.com/eToGA

Noticed how if the preventDefault is called on a later state it will not take effect and if it is called on time it will prevent dropping of the item.

I can suggest ensuring that the preventDefault will be called on time.

Regards,
Stefan
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Sortable
Asked by
Grant
Top achievements
Rank 2
Veteran
Iron
Answers by
Stefan
Telerik team
Share this question
or