Skip to content

Instantly share code, notes, and snippets.

@bhurlow
Created August 17, 2012 18:15
Show Gist options
  • Save bhurlow/3381259 to your computer and use it in GitHub Desktop.
Save bhurlow/3381259 to your computer and use it in GitHub Desktop.
Backbone Drag and Drop (quick and dirty)
DragDrop = Backbone.View.extend {
render: () ->
thisView = this
dragArea = @options.drag
dropArea = @options.drop
dropAreaY = $(dropArea).offset().top
dropAreaX = $(dropArea).offset().left
$dragAreaChildren = $(dragArea).children()
$dragAreaChildren.on 'touchstart', (e) ->
e.preventDefault()
movingChild = $(e.target)
movingChild.on 'touchmove', (e) ->
e.preventDefault()
if(e.touches.length == 1)
touch = e.touches[0]
node = touch.target
node.style.position = "absolute"
node.style.color = "red"
node.style.left = touch.pageX + "px"
node.style.top = touch.pageY + "px"
movingChild.on 'touchend', (e) ->
releasePositionX = e.changedTouches[0].pageX
releasePositionY = e.changedTouches[0].pageY
if releasePositionX >= dropAreaX and releasePositionY >= dropAreaY
childText = $(this).text()
$(dropArea).append '<h1>' + childText + '</h1>'
$(this).hide()
}
<div id='dragArea'>
<h1> Item 1 </h1>
<h1> Item 2 </h2>
<h1> Item 3 </h3>
</div>
<div id='dropArea'></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment