Ember.js + jQuery UI Draggable Clone

左心房为你撑大大i 提交于 2019-12-29 04:47:09

问题


I am trying to use Ember.js in conjunction with jQuery UI's draggable functionality, but I am encountering problems. Specifically, when using the clone helper, I am not able to drop the element and everything is extremely laggy. If I don't use the clone helper, everything works as expected.

I suspect this is related to jQuery UI cloning the html, including all of the metamorph script tags (used for binding).

I don't need to update the element live while I am dragging it. Is there a way to strip binding tags with ember?

For reference, here is the view logic:

didInsertElement: ->
  @_super()
  @$().draggable
    cursor: 'hand'
    helper: 'clone'
    opacity: 0.75
    scope: @draggableScope
  @$().droppable
    activeClass: 'dropActive'
    hoverClass: 'dropHover'
    drop: @createMatch
    scope: @droppableScope

My first thought was to try and use a beginPropertyChanges and endPropertyChanges during the drag to prevent an unexpected behavior. This doesn't seem to work nor is it ideal as I would like other bindings to update. Here is the revised code where I attempted this:

didInsertElement: ->
  @_super()
  @$().draggable
    cursor: 'hand'
    helper: 'clone'
    opacity: 0.75
    scope: @draggableScope
    start: ->
      Ember.beginPropertyChanges()
    stop: ->
      Ember.endPropertyChanges()
  @$().droppable
    activeClass: 'dropActive'
    hoverClass: 'dropHover'
    drop: @createMatch
    scope: @droppableScope

Any help would be greatly appreciated.


回答1:


I got this working by stripping all ember related metadata manually. Here is a small jquery plugin I whipped up:

# Small extension to create a clone of the element without
# metamorph binding tags and ember metadata

$.fn.extend
  safeClone: ->
    clone = $(@).clone()

    # remove content bindings
    clone.find('script[id^=metamorph]').remove()

    # remove attr bindings
    clone.find('*').each ->
      $this = $(@)
      $.each $this[0].attributes, (index, attr) ->
        return if attr.name.indexOf('data-bindattr') == -1
        $this.removeAttr(attr.name)

    # remove ember IDs
    clone.find('[id^=ember]').removeAttr('id')
    clone

To get it to work, just set the helper as follow:

helper: ->
  $this.safeClone()



回答2:


I was having the same issue using Ember 1.0.0 RC6. I've found that just replacing the clone string with a function which returns the clone works fine.

  this.$().draggable({
    // helper: 'clone'
    helper: function() {
      return $(this).clone();
    }
  });

In Coffeescript

@$().draggable
    # helper: 'clone'
    helper: ->
        $(@).clone()


来源:https://stackoverflow.com/questions/8797632/ember-js-jquery-ui-draggable-clone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!