Jason
Jason

Reputation: 1104

jQuery UI's sortable and backbone.js

I have a backbone.js project I have been working on, and I have it setup so that I can drag and drop rows (which are backbone.js models) and with the help of the jQuery UI update event I am able to make my models re-figure their order and everything is well. I was wondering if anyone new a cleaner way to make this happen. I have included some code below.

$( ".section" ).sortable({items: 'tr', update: function()
{
    console.log("Event Fire!");
    secv.mySort();
}});

secv is my View for the model that holds the table. The mySort function goes through and figures out the order of the elements and does the necessary updating.

Upvotes: 7

Views: 4471

Answers (1)

Bill Eisenhauer
Bill Eisenhauer

Reputation: 6183

I'm presuming you are setting the collection property in the View perhaps in the initialize method. In that same method, you should bind a view method to the collection's 'change' or 'refresh' event. This method would simply redraw the sorted collection; sorting the collection prior to doing so if necessary.

In theory, your model would have potentially updated itself with its new position and if the collection has a comparator function, the collection would automatically resort itself. If this is the case, binding to the 'refresh' event of the collection would trigger the above-mentioned method which needs only to re-render the collection portion of the view.

Upvotes: 1

Related Questions