nested_form和twitter typeahead

时间:2014-08-29 18:04:13

标签: ruby-on-rails twitter-typeahead

我已经在我的应用中实现了nested_form gem和twitter类型。但我无法使用typeahead来处理动态添加的字段。

我添加动态线条的行是     <%= ff.link_to_add t(" add_author"),:book_authors,:data => {:target => "#authors"}%>

第一行类型的JS是:

$(function(){
  var authors = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('author'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    remote: {
      url: '/load_suggestions?q=%QUERY',
      filter: function(list) {
        return $.map(list, function(author) { return {author: book }; });
      }
    }
  });

  authors.initialize();

  $('#load_suggestions').typeahead({
      hint: true,
      highlight: true,
      minLength: 1
  },
  {
      name: 'authors',
      displayKey: 'author',

      source: authors.ttAdapter()
  });
});

如何修改以上内容以适应动态添加的行?感谢

1 个答案:

答案 0 :(得分:0)

您可以在添加字段后收听点击事件和激活预订。 处理javascript事件在嵌套表单gem的自述文件中给出。

e.g。

 $(document).on('nested:fieldAdded', function(event){
      // this field was just inserted into your form
      var field = event.field; 
      field.typeahead({
        hint: true,
        highlight: true,
        minLength: 1
      },
      {
         name: 'authors',
         displayKey: 'author',

         source: authors.ttAdapter()
     });
});