10

Is there an example on how to implement Drag and Drop with Ember.js ? I have tried using jQuery UI, but the integration seems to be somewhat complex.

I've seen this jsFiddle: http://jsfiddle.net/oskbor/Wu2cu/1/ but haven't been able to implement this successfully in my own app.

What are the options for a rather simple drag&drop implementation using Ember.js ?

Joachim H. Skeie
  • 1,893
  • 17
  • 27

1 Answers1

17

I took a look at the post by Remy Sharp and implemented a basic example in Ember.js, see http://jsfiddle.net/pangratz666/DYnNH/.

Handlebars:

<script type="text/x-handlebars" >
    Drag and drop the green and red box onto the blue one ...

    {{view App.Box class="box green"}}
    {{view App.Box class="box red"}}

    {{view App.DropTarget class="box blue"}}
</script>​

JavaScript:

DragNDrop = Ember.Namespace.create();

DragNDrop.cancel = function(event) {
    event.preventDefault();
    return false;
};

DragNDrop.Dragable = Ember.Mixin.create({
    attributeBindings: 'draggable',
    draggable: 'true',
    dragStart: function(event) {
        var dataTransfer = event.originalEvent.dataTransfer;
        dataTransfer.setData('Text', this.get('elementId'));
    }
});

DragNDrop.Droppable = Ember.Mixin.create({
    dragEnter: DragNDrop.cancel,
    dragOver: DragNDrop.cancel,
    drop: function(event) {
        var viewId = event.originalEvent.dataTransfer.getData('Text');
        Ember.View.views[viewId].destroy();        
        event.preventDefault();
        return false;
    }
});

App.Box = Ember.View.extend(DragNDrop.Dragable);
App.DropTarget = Ember.View.extend(DragNDrop.Droppable);​
pangratz
  • 15,875
  • 7
  • 50
  • 75
  • This looks promising, and similar to what I have in my own app. But it won't really support browsers that do not support the HTML5 draggable and droppable attributes, which is what I am after here. Obviously I'm most concerned with IE support :) – Joachim H. Skeie May 25 '12 at 11:05
  • Hmm, it looks like it's supported in IE, see http://caniuse.com/#feat=dragndrop ... – pangratz May 25 '12 at 12:36
  • 1
    could you update your fiddle with jquery 1.7 ? It seems to be broken using jQuery edge. – sly7_7 Jul 29 '12 at 19:55
  • @sly7_7 Wow, thanks for the catch!! I've updated the code to use 1.7.2. – pangratz Jul 29 '12 at 19:59
  • No problem, just a question of luck, finding this answer for answering an other one :) – sly7_7 Jul 29 '12 at 20:10