Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 708
Jason Avatar asked Jul 05 '11 21:07

Jason


1 Answers

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.

like image 80
Bill Eisenhauer Avatar answered Oct 18 '22 08:10

Bill Eisenhauer