2012-01-10 115 views
9

我想用Ember.js和jQuery UI的可拖动功能一起使用,但是我遇到了问题。具体来说,当使用帮助器时,我无法删除元素,一切都非常缓慢。如果我不使用克隆助手,一切都按预期工作。Ember.js + jQuery UI可拖动的克隆

我怀疑这与jQuery UI克隆html,包括所有metamorph脚本标记(用于绑定)有关。

我不需要在拖动它时更新现场元素。有没有办法用ember去掉绑定标签?

仅供参考,这里是视图逻辑:

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

我首先想到的是试图使用beginPropertyChangesendPropertyChanges拖动过程中,以防止意外的行为。这似乎不起作用,也不是很理想,因为我希望其他绑定更新。这里是我试图修改的代码:

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 

任何帮助将不胜感激。

回答

9

我通过手动去除所有与花费有关的元数据来得到这个工作。这里是一个小的jQuery插件我掀起了:

# 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 

为了得到它的工作,刚才设置的帮手如下:

helper: -> 
    $this.safeClone() 
+0

那么,你如何重新启用绑定后?或者你不关心这个吗? – 2012-01-10 03:05:40

+0

仅在用于拖拽帮助程序的克隆元素中禁用绑定。原始元素保持完好 – ghempton 2012-01-10 03:16:40

+0

啊,有你了。那么我想不出任何内置的东西来做你正在尝试做的事情。你看起来像一个干净的解决方案给我。 – 2012-01-10 03:24:57

1

我在使用灰烬1.0.0 RC6同样的问题。我发现只需用一个返回克隆的函数替换克隆字符串就可以正常工作。

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

在CoffeeScript的

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