backbone.js 将数据从simple_form发送到javascript

qfe3c7zg  于 2022-11-10  发布在  Java
关注(0)|答案(1)|浏览(114)

我有一个工作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'

它搜索客户端并列出与查询匹配的客户端。
我想在一个普通的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'

但是我不知道如何把标签放到javascript中...
看起来工作代码中相关的部分是text_field_tag = 'search_term',这看起来是将文本字段与javascript关联起来的部分。但是我不知道如何将它与simple_form一起使用...
下面的咖啡脚本,如果它是重要的:

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()

预计到达时间:简化:我想我需要两样东西:
1.我需要将simple_form的类设置为“search_form”
1.我需要执行与text_field_tag行等效的操作,但针对的是f.输入行。
我很确定第一个应该只是在simple_form的html部分添加class: "search_form"
但是,我想不出如何设置“text_field_tag

dtcbnfnu

dtcbnfnu1#

对于将来的搜索者:看起来我是正确的,我需要设置simple_form的类,并且需要设置f.input的“名称”。
input_html: {name: 'search_term'}选项添加到f.输入行可以解决这个问题。
然而,这也意味着,在控制器中,值不是以params.domainNameSwap.oldDomainName表示,而是以params.search_term表示,这是一个我可以接受的问题...

相关问题