简体   繁体   English

将数据从simple_form发送到javascript

[英]Sending data from simple_form to javascript

I have a working javascript: 我有一个有效的JavaScript:

 .search_client_users
    = form_tag admin_clients_path, method: "get", class: "search_form" do
      = label_tag 'search_term', 'Search for client users:'
      = text_field_tag 'search_term', nil, autocomplete: "off", size: "50"

    .client_list

- content_for :javascript do
  = javascript_include_tag 'admin/search_client_users'

which does a search of clients and lists out the clients that match the query. 搜索客户端并列出与查询匹配的客户端。

I want to use this in a normal simple_form: 我想在普通的simple_form中使用它:

    .main_form.client_emails
      = simple_form_for(:domainNameSwap, url: { action: "update" }, html: { method: :put }) do |f|
        .input-row
          = f.input :oldDomain, :label => "Domain Name",wrapper_html: { id: 'search_term' }
        .submit-row
          .col-xs-5
            .submit
              = f.submit "Update domains", id: "submit", :class => "btn btn-primary submit"

    .client_list


- content_for :javascript do
  = javascript_include_tag 'admin/search_client_users'

But I can't figure out how to get the label to the javascript... 但我不知道如何将标签添加到javascript ...

it appears that the part in the working code that is relevant is text_field_tag = 'search_term' That appears to be what associates the text field with javascript. 似乎工作代码中相关的部分是text_field_tag = 'search_term'这似乎是将文本字段与javascript相关联的部分。 But I can't figure out how to use that with simple_form... 但是我不知道如何在simple_form中使用它...

coffeescript below, in case it's important: 如果重要的话,请在下面的coffeescript中:

class App.ClientUserList
  constructor: ->
    @incrementalSearchAttempts = 0

  search: (searchTerm, completeCallback) =>
    handleResponseWithOrderAwareness = (attemptNumber, response) =>
      if attemptNumber >= @incrementalSearchAttempts
        completeCallback(response)

    @incrementalSearchAttempts++
    onComplete = _.partial(handleResponseWithOrderAwareness, @incrementalSearchAttempts)
    $.get('/admin/manage_clients/client_list', { search_term: searchTerm }).complete(onComplete)

class App.Views.SearchClientUsers extends Backbone.View
  events:
    "keyup input[name='search_term']": "search",
    "click .profile_attribute": "showClientUserProfile"

  initialize: =>
    @clientUserList = new App.ClientUserList()

  search: =>
    searchTerm = $('.search_form input[name=search_term]').val()
    @clientUserList.search(searchTerm, @render)

  showClientUserProfile: (event) =>
    window.location = $(event.currentTarget).closest('tr').data('client-path')

  render: (response) =>
    @$el.find('.client_list').html(response.responseText)

$ ->
  new App.Views.SearchClientUsers(el: $('.search_client_users')).search()

ETA: simplified: I think I need two things: ETA:简化:我认为我需要两件事:

  1. I need to set the class of the simple_form to "search_form" 我需要将simple_form的类设置为“ search_form”
  2. I need to do the equivalent of the text_field_tag line, but for the f.input line. 我需要做等效于text_field_tag行,但对于f.input行。

I'm pretty sure that the first SHOULD be just adding class: "search_form" in the html section of the simple_form. 我很确定第一个应该只是在simple_form的html部分添加class: "search_form"

However, I can't figure out how to set the " text_field_tag " 但是,我不知道如何设置“ text_field_tag

For future searchers: it appears that I was correct, I need to set the class of the simple_form, and I need to set the "name" of the f.input. 对于将来的搜索者:看来我是正确的,我需要设置simple_form的类,并且需要设置f.input的“名称”。

adding the input_html: {name: 'search_term'} option to the f.input line solves this. input_html: {name: 'search_term'}选项添加到f.input行即可解决此问题。

HOWEVER, it also means that instead of, in the controller, the value being in params.domainNameSwap.oldDomainName , it's in params.search_term Which is a problem that I can live with... 但是,这也意味着该值不是在控制器中,而是在params.domainNameSwap.oldDomainName ,而是在params.search_term这是我可以忍受的一个问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM