I've got a backbone view model that I'm rendering here and making it draggable with jquery ui.
render: -> $(this.el).attr('class', 'item').html(this.template(this.options.model.toJSON() )) viewmodel = this $(this.el).draggable revert: true drag: () -> console.log(viewmodel)
Above, I have viewmodel available and can remove it from the dom, call methods on its model, etc. But what I want is to drag this view model into a droppable container --like a trash can-- and then call a few of the view model's methods and remove it from the DOM.
What I see though, is the callback method for when an item is dropped into a container would be:
$(function() { $("#trash").droppable({ drop: function(event, ui) { console.log(ui.draggable); } }); });
So, I'm able to see ui.draggable and remove it from the DOM, but i have no reference to its view model. Am I doing something wrong? Any way to work around this?
Check whether your draggable object is already loaded in the viewport. If it is not, it won't work properly. JUST AFTER the draggable object to be absolutely sure that everything is loaded at the correct time. When you'll be sure everything is ok, then you'll be able to refactor.
Draggable() Method This method allows the elements to be dragged with the help of mouse. Using jQuery UI, we can make the DOM(Document Object Model) elements to drag anywhere within the view port. This can be done by clicking on the draggable object by mouse and dragging it anywhere within the view port.
You can use the Backbone. Model without jQuery, but Backbone. View will require either jQuery or Zepto, just like the docs state. Chances of not using view and router is low enough.
$ (selector, context). droppable (options) Method. The droppable (options) method declares that an HTML element can be used as an element in which you can drop other elements. The options parameter is an object that specifies the behavior of the elements involved.
I think I ran into the same issue; instead of adding meta-data to the element or storing it globally, I just stored a reference to the actual view itself on the DOM element, which then gives you access to the model, and any info you need from there.
window.MyDraggableView = Backbone.View.extend({ initialize: function(){ $(this.el).draggable(); $(this.el).data("backbone-view", this); } }); window.MyDropTarget = Backbone.View.extend({ initialize: function(){ $(this.el).droppable({ drop: function(ev, ui){ // get reference to dropped view's model var model = $(ui.draggable).data("backbone-view").model; }, }); }, });
I've had this problem. I solved it thusly: Give the drop target a reference to the model collection. Set a property data-cid="<%= cid %>"
on the draggable. Now you can look up the model in the collection from the $(ui.draggable).data('cid')
. Since backbone asserts that CIDs are unique, you could even scan a collection of collections, in case there were multiple model classes you wanted to be trashable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With