rails,使用咖啡脚本出现语法错误 [英] rails, getting syntax error with coffee script
问题描述
我正在尝试使用 jquery ui 库在 rails 中执行自动完成功能.但是我不断收到语法错误语法错误:在线保留字函数"......"
i am trying to do an autocomplete feature in rails using the jquery ui library. however i keep getting syntax errors "Syntax Error: reserved word "function" on line ..."
这是我的课程.js.coffee 文件
this is my lessons.js.coffee file
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;
}
});
});
我在某处在线阅读,我可以用 -> 替换单词 function我这样做了,我停止接收函数错误,但随后我收到其他语法错误,例如语法错误:在线保留字var"......"
i read online somewhere that i could replace the word function with -> i did that and i stopped receiving the function errors, but then i get other syntax errors such as "Syntax Error: reserved word "var" on line..."
我做错了吗?
推荐答案
只有第一行是coffeescript;其余的都是普通的javascript.
Only the first line is coffeescript; the rest is normal javascript.
尝试使用此转换器:
$(function() {});
变成 $ ->
这篇关于rails,使用咖啡脚本出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!