翻译jQuery到Coffeescript [英] Translating jQuery to Coffeescript

查看:216
本文介绍了翻译jQuery到Coffeescript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前发过一个混乱的问题。长篇小说,我有一点jQuery工作接近我想要的方式,但我不能为我的生活得到它在CoffeeScript工作。



当我尝试它不同的方式在CS的搜索事件触发器,一旦页面加载。我不能像我在jQuery中一样附加到dataTable。



我确定它是一个简单和蠢的。



这是代码:

  $(document).ready(function(){
$('#customers')。dataTable({
sPaginationType:bootstrap,
aaSorting:[],
ordering:false
} ;

$('#customers')
.on('search.dt',function(){$('。nested_indent')。hide();})

});

最新的CS版本在这里(每次加载页面时都会启动) p>

  jQuery  - > 
$('#customers')。dataTable(
sPaginationType:bootstrap
aaSorting:[]
ordering:false
)。 $('#customers')。on('search.dt',
$('。nested_indent')。hide())

解决方案

从JavaScript版本到结尾有两个差异:

 )。$('#customers')。on('search.dt',
$('。nested_indent')。hide())

第一个是。之前 $ ')。这试图使用 $ 作为方法而不是全局。您将要删除并插入一个具有匹配缩进的换行符。



第二个是函数 $('。nested_indent')。hide()您必须至少包括 - > 来定义一个,就像您拥有 jQuery - > 一样。

 )#1)具有换行符的单独语句

$('#customers')。on ('search.dt', - >#2)在`function`中包装`hide`
$('。nested_indent')。hide())
/ pre>




第二个区别是为什么会看到:




[...]

如果没有 - > ,则会立即调用 .hide()语句, $ c> return 值被传递给 .on(),因此它实际上并不绑定到事件。


I posted a confusing question before. Long story short, I have a bit of jQuery working close to the way I want, but I cannot for the life of me get it to work in CoffeeScript.

When I try it different ways in CS the "on search" event triggers as soon as the page is loaded. I can't get it to just attach to the dataTable as I do in jQuery.

I'm sure it's something simple and dumb.

Here's the code:

$(document).ready(function() {
    $('#customers').dataTable( {
        "sPaginationType": "bootstrap",
        "aaSorting": [],
        "ordering": false
    } );

    $('#customers')
            .on( 'search.dt',  function () { $('.nested_indent').hide(); } )

} );

The most recent CS version is here (the one that trips every time the page loads.)

jQuery ->
    $('#customers').dataTable(
        "sPaginationType": "bootstrap"
        "aaSorting": []
        "ordering": false
    ).$('#customers').on( 'search.dt',  
         $('.nested_indent').hide() )

解决方案

There are 2 differences towards the end from the JavaScript version:

).$('#customers').on( 'search.dt',  
     $('.nested_indent').hide() )

The first is the . before $('#customers'). This attempts to use $ as a method rather than a global. You'll want to remove the . and insert a line break with matching indentation.

The second is that the function that was around $('.nested_indent').hide() is missing. You'll have to include at least -> to define one, as you have with jQuery ->.

)  # 1) separate statements with line break

$('#customers').on( 'search.dt', ->  # 2) wrap `hide` in a `function`
     $('.nested_indent').hide() )


The 2nd difference is why you're seeing:

[...] the "on search" event triggers as soon as the page is loaded.

Without the ->, the .hide() statement is being called immediately and its return value is being passed to .on(), so it isn't actually bound to the event.

这篇关于翻译jQuery到Coffeescript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆