2012-10-08 73 views
0

我决定在项目中使用NicEdit,因为它是轻量级的。NicEdit - 解除绑定事件

所以,现在我的页面中有可变数量的实例,点击加载并在编辑器模糊处移除。

我需要知道如何从这个组件解除绑定事件。我试图手动解除绑定,但我不明白他们在哪里链接!

$('.container').bind('click', function(){ 
    var _form = $(this).parentsUntil('form').parent(); 
    var textarea = _form.find('textarea.edit'); 
    var ta_id = textarea.attr('id'); 
    var ed = new nicEditor(niceditOptions).panelInstance(ta_id); 

    // Show Preview and update textarea and so on 
    ed.addEvent('blur', function() { 
     var _ed = nicEditors.findEditor(ta_id); 
     var ev_type, evt, events = this.eventList; 

     for (ev_type in events){ 
      for (evt in ev_type){ 
       if (this.removeEventListener){ 
        this.removeEventListener(ev_type, events[ev_type][evt]); 
       } 
       else { 
        this.detachEvent('on' + ev_type, events[ev_type][evt]); 
       } 
      } 
     } 
     this.removeInstance(ta_id); 
    }); 

}); 

非常感谢!达维德。

+0

我不明白,你正试图解开别的东西?你能否为你想达到的目标增加一些清晰度? – JDandChips

+0

当然!我有多个textareas。我激活编辑器点击textarea并关闭模糊处理器(nicEdit blur)。当我删除当前实例时,单击编辑器外部(nicEdit blur),blur事件仍然绑定在某处,并在控制台日志中进行检查。点击另一个textarea,我建立另一个编辑器并附加另一个模糊事件。我不明白组件是如何工作的,或者在删除实例时有什么脏东西? – Davide

+0

是的,所以它听起来像问题是,你正试图创建一个新的编辑器面板,每次你选择一个文本框?你有没有尝试过使用一个编辑器面板?您可以在解除绑定时将其“隐藏”,在点击时将其“隐藏”,然后您只需对着编辑器的单击实例重新“定位”即可。这通常是我的做法,因为nicEditor在一个编辑器面板实例中运行得非常好。如果你认为这可以解决你的问题,我可以写一个例子。 – JDandChips

回答

0

还有其他方法可以解决您的问题,但在这种情况下,我更愿意使用一个版本的nicEditor面板并绑定我所有的所见即所得实例。原因是我认为它稍微整齐一些。我会假设你知道如何bind one editor to multiple editable instances

负载我的HTML可能会是这个样子:

<div id="instance1">text</div> 
... 
<div id="instance2">text</div> 
... 
<div id="myNicPanel" style="display:none;position:relative;"></div> 

所以,一旦页面已经完成它的负载周期,我应该有两个可编辑区域和一个隐藏的编辑器。然后我会用下面的jQuery的重新定位,并显示在编辑器当选择一个实例编辑:

 $('#instance1 , #instance2').click(function() { 
     //Reposition the editor to just above the selected instance 
      $('#myNicPanel').css({ 
       top: $(this).position().top, 
       left: $(this).position().left, 
       display: 'block', 
       width: $(this).width() - 2 //manual adjustment, 
       position: 'absolute' 
      }); 
     //Make the width of the editor equal to that of the instance 
      $('#myNicPanel').css({ 
       top: $(this).position().top - $('#myNicPanel').height() 
      }); 
     }); 

你当然会已经开始你的编辑器,并在此之前情况下,如果你也想拥有编辑器再次隐藏模糊,您可以将hide()函数附加到nicEditor events之一。

+0

在我的情况下,这种方法使用户体验比多个实例更清晰。 我必须了解如何通过点击保存点击来触发自定义事件,以更新内容..非常感谢! – Davide