Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag & Drop doenst not work: 'dropEffect' of undefined

Uncaught TypeError: Cannot set property 'dropEffect' of undefined main.js
Uncaught TypeError: Cannot read property 'files' of undefined

What's wrong here

.coffee

$ ->
  app = new Viewr

class Viewr

  constructor: () ->
    $('#drop_zone')
    .bind('dragover', @handleDragOver)
    .bind('drop', @handleDrop)

  handleDrop: (evt) ->
    evt.stopPropagation()
    evt.preventDefault()
    files = evt.dataTransfer.files

    @setActiveImage files[0]

  handleDragOver: (evt) ->
    evt.stopPropagation()
    evt.preventDefault()
    evt.dataTransfer.dropEffect = "copy"

  setActiveImage: (image)->
    $("img").attr "src", src

.Html

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
    <div id="drop_zone" style="width: 100px; height: 100px; border: 2px dashed #bbb;
                                 -moz-border-radius: 5px;
                                 -webkit-border-radius: 5px;
                                 border-radius: 5px;
                                 padding: 25px;
                                 text-align: center;
                                 color: #bbb;">Drop files here</div>
</body>
<script type="text/javascript" src="js/lib/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</html>
like image 266
fabian Avatar asked Feb 09 '13 13:02

fabian


1 Answers

I think the problem may be that you are using the jQuery 'dragover' and 'drop' event rather than the native drop event. As you can see here, it mentions having to add the DataTransfer object to the jQuery event object:

OtherProperties

Certain events may have properties specific to them. Those can be accessed as properties of the event.originalEvent object. To make special properties available in all event objects, they can be added to the jQuery.event.props array. This is not recommended, since it adds overhead to every event delivered by jQuery.

Example:

// add the dataTransfer property for use with the native `drop` event
// to capture information about files dropped into the browser window
jQuery.event.props.push("dataTransfer");

Your best bet is probably to access the originalEvent object, so see if this works:

handleDrop: (evt) ->
    evt.stopPropagation()
    evt.preventDefault()
    files = evt.originalEvent.dataTransfer.files

handleDragOver: (evt) ->
    evt.stopPropagation()
    evt.preventDefault()
    evt.originalEvent.dataTransfer.dropEffect = "copy"
like image 129
phenomnomnominal Avatar answered Sep 18 '22 09:09

phenomnomnominal