rails,使用咖啡脚本获取语法错误

时间:2012-04-17 09:12:19

标签: jquery ruby-on-rails autocomplete coffeescript

我正在尝试使用jquery ui库在rails中执行自动完成功能。但是我继续得到语法错误“语法错误:保留字”功能“在线...”

这是我的课程.js.coffee文件

jQuery ->

$(function() {
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }

    $( "#lesson_tag_name" )
        // don't navigate away from the field on tab when selecting an item
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: $('#lesson_tag_name').data('autocomplete-source')
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            }
        });
});

我在网上读过一些我可以用 - &gt;替换单词功能的地方 我这样做了,我停止接收功能错误,但后来我得到其他语法错误,如“语法错误:保留字”var“在线......”

我做错了什么?

2 个答案:

答案 0 :(得分:16)

只有第一行是coffeescript;其余的都是正常的javascript。

尝试使用此转换器:

http://js2coffee.org/

$(function() {});变为$ ->

答案 1 :(得分:3)

如果你想在一个coffeescript文件中嵌入javascript,你可以使用反引号来实现:

jQuery ->
  `function abc() { return 123; }  // js syntax here`

见这里:http://coffeescriptcookbook.com/chapters/syntax/embedding_javascript

这非常令人困惑,因此将代码转换为coffeescript通常是个更好的主意,在这种情况下,您可以像DanS建议的那样使用js2coffee.org转换器。