2015-04-03 106 views
0

鉴于以下的CoffeeScript为什么不会选择2个事件正确触发?

class ModalController 
    constructor: (modalClass) -> 
    @modalClass = modalClass 
    @enableSelect2() 
    @clearSelectOptions() 

    enableSelect2:() -> 
    $('.select2-init').select2() 
     .on 'select2-open', (e) -> 
     console.log e 
     .on 'select2:selecting', (e) => 
     console.log 'added' 
     @validatePresenceOf($(e.target)) 

我希望,当我专门做了一个选择上面的代码中调用.on 'select2:selecting应该火行,这是高清并非如此,因为在下面的代码片段被记录在案。

https://cl.ly/image/2P3d3F0y3k1o/Screen%20Recording%202015-04-03%20at%2010.38%20AM.gif

我立足我这一关的理解选择2文档

https://cl.ly/image/2Z3k0q2b2H1V/Image%202015-04-03%20at%2010.40.37%20AM.png

如何解决这个问题?

重新在事件选择可能的错字,

下面的CoffeeScript

enableSelect2:() -> 
    $('.select2-init').select2() 
     .on 'change', (event) => 
     if event.added 
      @validatePresenceOf($(event.target)) 
     .on 'select2:selecting', (e) => 
     console.log "selecting" 
     .on 'select2:select', (e) => 
     console.log "select" 
     .on 'select2-removed', (e) => 
     @validatePresenceOf($(e.target)) 

产生以下,注意到没有日志报表(虽然我已经通过使用砍死我要找的验证该event.added检查。

https://cl.ly/image/1l3Y3h1a1e2t/Image%202015-04-03%20at%2011.05.41%20AM.png

模式select2:eventname不做任何事情,即没有日志,没有挂钩。这很奇怪。我在互联网上发现的一个随机网站上发现了select2-eventname模式。这对我来说太奇怪了,令人不安,这看起来像是一段简单的代码。

+0

是如此不可思议。你可以尝试在http://jsfiddle.net上复制它吗? – Dhiraj 2015-04-03 18:17:13

+0

您正在使用Select2 3和4事件的组合,您实际使用的是哪个版本? – 2015-04-05 16:49:59

回答

5

出现使用选择二3成.x,但是您正在引用Select2 4.0.0文档中的内容。

选择二4.0.0文档:https://select2.github.io/
选择二3.5.2文档:http://select2.github.io/select2/

还有上链接到选择二3.5.2文档主页的链接。


您正在使用select2-open这似乎是工作,是3.5.2的事件,但你也尝试使用select2:selecting,这是一个4.0.0事件。 select2:selecting的Select2 3.5.2事件是select2-selecting(请注意-而不是:)。在活动文档

  • 用于包含addedremoved属性,当添加和删除的东西检查change看时

    其他显着的变化。现在你应该使用select2:select事件来检查什么时候被添加,并且select2:unselect什么时候被删除。

  • select2-selecting现在select2:selecting
  • select2-removing现在select2:unselecting
  • select2-removed现在select2:unselect
  • select2-opening现在是现在select2:opening
  • select2-openselect2:open
+0

#derp谢谢:) – John 2015-04-06 17:43:45

0

可能的错字。应该说是

。对 '选择2,选择' ...

代替

。对 '选择2:选择' ......

+0

是啊,我试过了,出于某种原因,没有与该模式的钩子被拾起。很奇怪。我在这个问题上加了背景来说明这一点。 – John 2015-04-03 18:09:45

相关问题