0

我能夠在CoffeeScript中使用jQuery自動完成功能,但是當我嘗試使用重疊摺疊進行操作時,發生了一些情況。這是設在jQuery的文檔與普通的JavaScript在CoffeeScript中使用重音摺疊的jQuery自動完成

$(function() { 

var names = [ "Jörn Zaefferer", "Scott González", "John Resig" ]; 

var accentMap = { 
    "á": "a", 
    "ö": "o" 
}; 
var normalize = function(term) { 
    var ret = ""; 
    for (var i = 0; i < term.length; i++) { 
     ret += accentMap[ term.charAt(i) ] || term.charAt(i); 
    } 
    return ret; 
}; 

$("#developer").autocomplete({ 
    source: function(request, response) { 
     var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); 
     response($.grep(names, function(value) { 
      value = value.label || value.value || value; 
      return matcher.test(value) || matcher.test(normalize(value)); 
     })); 
    } 
}); 
}); 

的例子,這是CoffeeScript的版本我做

jQuery -> 
    names = [ 
     "Jörn Zaefferer", 
     "Scott González", 
     "John Resig" ] 

    accentMap = { 
     "á": "a", 
     "ö": "o" 
    } 

    normalize = (term) -> 
     ret = "" 
     for i in [0..term.length] 
      ret += accentMap[term.charAt i] || term.charAt i 
     return ret 

    $("#search_word").autocomplete { 
     source: (request, response) -> 
      matcher = new RegExp $.ui.autocomplete.escapeRegex (request.term), "i" 
      response $.grep names, (value) -> 
       value = value.label || value.value || value 
       (matcher.test value) || (matcher.test normalize value) 
    } 
+0

你可以試試: http://js2coffee.org/ – Guillaume86

+0

這實際上比你想象的更有幫助。如果它不是我的帖子的評論,我會讓你作爲答案。 – KayleL

+0

所以我做了這個答案;)很高興它是有用的 – Guillaume86

回答