【问题标题】:Rails 3.1 Remote Button Unobtrusive Javascript event not caught (JQuery)Rails 3.1 Remote Button Unobtrusive Javascript 事件未捕获(JQuery)
【发布时间】:2011-11-26 11:50:40
【问题描述】:

我想开始使用 Ajax 事件 ajax:success、ajax:failure、ajax:complete 和 ajax:beforeSend 推荐用于不显眼的 Javascript,例如:

但由于某种原因,它对我不起作用。我错过了一些东西(一些小东西),因为我无法让事件触发我的 Javascript。我希望有人能在我的代码中发现“明显”的错误/遗漏。

关于我的环境的一些细节:

  • Rails 3.1
  • jQuery (jquery-rails gem)
  • 用于服务器端 js 处理的 therubyracer(这对我的示例并不重要)

为了尝试找出我缺少的东西,我创建了一个简单的测试应用程序,它有一个远程按钮。单击按钮时,我希望触发警报框。这个应用的代码可以在github上看到:

http://github.com/jpmcgrath/rbtest

我已经将应用部署到heroku这里:

http://rbtest.heroku.com/projects/

如果你看app,可以点击按钮,按钮成功创建新项目(看到手动刷新),但是ajax:success事件好像没有发生?

代码内容如下:

在 projects_controller.rb 中

def remote_test
  @project = Project.new(:name => "remote test")

  respond_to do |format|
    if @project.save
      puts "project saved!\n\n\n"
      format.html { redirect_to @project, notice: 'Project was successfully created.' }
      format.json { render json: @project, status: :created, location: @project }
    else
      format.html { render action: "new" }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

在 application.js 中

jQuery(document).ready(function() {

  jQuery(".remote_button").click(function() {
    alert('Handler for .click() called.');
  })
  .bind("ajax:complete", function() {
    alert('complete!');
  })
  .bind("ajax:beforeSend", function () {
    alert('loading!');
  })
  .bind("ajax:error", function (xhr, status, error) {
    alert('failure!');
  })
  .bind('ajax:success', function(event, data, status, xhr) {
    alert('success!');
  });
});

在视图projects/index.html.erb

<%= button_to "remote test", remote_test_path, :remote => true, :class => 'remote_button'  %>

如果有人能指出我遗漏了什么(我怀疑它与响应类型有关),将不胜感激。

【问题讨论】:

    标签: ruby-on-rails-3 jquery ruby-on-rails-3.1 unobtrusive-javascript


    【解决方案1】:

    您的事件处理程序未触发的原因是您的选择器位于错误的元素上。 button_to 创建一个带有单个输入标记的表单,它是具有您选择的类的输入标记,但是在表单上触发了 ajax 事件,而不是在输入标记上。

    试试这个

    jQuery(document).ready(function() {
    
      jQuery(".remote_button").click(function() {
        alert('Handler for .click() called.');
      });
    
      jQuery("form.button_to").bind("ajax:complete", function() {
        alert('complete!');
      })
      .bind("ajax:beforeSend", function () {
        alert('loading!');
      })
      .bind("ajax:error", function (xhr, status, error) {
        alert('failure!');
      })
      .bind('ajax:success', function(event, data, status, xhr) {
        alert('success!');
      });
    });
    

    我在我的 firebug 控制台中做了同样的事情,当回调出现在表单上时,它触发了 ajax:success 处理程序。

    要更改表单上的类,请使用 :form_class 选项将其从 button_to 更改为其他内容

    <%= button_to "remote test", remote_test_path, :remote => true, :class => 'remote_button', :form_class => 'remote_button_form' %>
    

    然后使用添加您的 ajax 回调

    jQuery(".remote_button_form").bind ...
    

    http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

    【讨论】:

    • 非常感谢,就是这样!此外,您的建议代码中有错字。 “.remove_button_form”应该是“.remote_button_form”。干杯!
    【解决方案2】:

    【讨论】:

    • 感谢您的回复。我知道这些变化,但我不认为这是我的问题的原因。对于我的示例应用程序,我避免使用咖啡脚本,因为我想简化。我使用的 js 似乎正在工作,因为单击按钮时会显示 js 警报,显示“调用了 .click() 的处理程序。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多