Twitter的先行者

时间:2014-08-21 14:34:29

标签: ruby-on-rails twitter typeahead

我正在尝试将twitter的typeahead插件添加到我的rails应用程序中。我已经下载了typeahead.bundle.js并将其放在vendor / assets / javascripts目录中。我已将// = require typeahead.bundle添加到app / assets / javascripts / application.js。然后我把第一个例子放在那里

<div id="the-basics">
  <input class="typeahead" type="text" placeholder="States of USA">
</div>

在我的一个观点和以下的javascript上  应用程序/资产/ Javascript角/ example.js

但是当我尝试输入字符时,没有任何反应,甚至在日志上也没有。谢谢你的帮助

var substringMatcher = function(strs) {
  return function findMatches(q, cb) {
    var matches, substrRegex;

    matches = [];

    substrRegex = new RegExp(q, 'i');

    $.each(strs, function(i, str) {
      if (substrRegex.test(str)) {

        matches.push({ value: str });
      }
    });

    cb(matches);
  };
};

var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

$('#the-basics .typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  displayKey: 'value',
  source: substringMatcher(states)
});

0 个答案:

没有答案