berkes
berkes

Reputation: 27553

How to trigger an "update" event for jQuery-sortable in JavaScript?

I want to test sorting made with jQuery sortable from within my integration tests. In order to do so, the testing-framework (capybara, selenium) allows me to call page.execute_script("activeAdminSortable().sortable().update()").

By triggering the update event with the proper parameters, in I can simulate a drag-and-drop to test if it works. The relevant parts of the JavaScript code is:

(function($) {
  $(document).ready(function() {
    $('tbody .grabber').parents('tbody').activeAdminSortable();
  });

  $.fn.activeAdminSortable = function() {
    this.sortable({
      update: function(event, ui) {
        $.ajax({
          axis: 'y',
          cursor: 'move',
          url: ui.item.find('[data-sort-url]').data('sort-url'),
          type: 'post',
          data: { position: ui.item.index() + 1 },
          success: activeAdminSortableFlashFinished
        });
      }
    });

    this.disableSelection();
  }
})(jQuery);

I need to find a way to trigger the update event and pass it the correct parameters. Is there any easy way to craft such an event, and ui parameter? And to trigger the update.

Alternatively, I could write some JavaScript that actually grabs, drags and drops the items, but that seems rather brittle to me. Or is that trivial?

Upvotes: 2

Views: 1978

Answers (2)

stevemarvell
stevemarvell

Reputation: 993

Add .trigger("update") where you have .update()

Upvotes: 2

jerrygarciuh
jerrygarciuh

Reputation: 21988

I am using $('#sortable').trigger('sortupdate') successfully for this same purpose.

Upvotes: 1

Related Questions