Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery UI drag/drop with backbone.js

One feature of my Backbone app involves associating models of type A with models of type B, which is done by dragging view A onto view B. In B's view class I listen for the drop event and from this I get the DOM element of view A, but no information about model A.

What's the best way to go about retrieving this information? My best guesses so far are

  • have model A save a reference to itself in the app's namespace, removing this reference on drag end if the drop handler hasn't already done so
  • fire an event on view A, passing a reference to model B along with the event, and then having model A call a method of model B...
  • store model A as a $.data attribute of view A

but all these approaches seem convoluted/inelegant.

like image 379
wheresrhys Avatar asked Feb 22 '12 22:02

wheresrhys


1 Answers

Storing as a data-attribute is actually quite clean, and the performance will not be bad. You can store the model's cid attribute as data-cid on the DOM el, and use the collection's getByCid method to retrieve the model.

like image 158
frontendbeauty Avatar answered Oct 04 '22 00:10

frontendbeauty