Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag&Drop with Ember.js

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 ?

like image 382
Joachim H. Skeie Avatar asked May 24 '12 14:05

Joachim H. Skeie


1 Answers

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);​
like image 131
pangratz Avatar answered Oct 23 '22 13:10

pangratz