jquery-ujs ajax 事件不被处理
我正在尝试使用 Rails 3 实现 ajaxy 注册。我正在使用 jquery-ujs 和远程表单。我使用 $.get
请求访问注册表单,并且它显示正确。此注册表单是远程的:
form_for @user, :remote => true, :html => {"data-type" => "html"} do |f|
在我的 application.js 中,如果获取表单的 ajax 请求成功,我会尝试从不显眼的 javascript 绑定 ajax 事件的处理程序:
var load_remote_form = function(evt) {
var href = $(this).attr('href');
// load form template
$.get(href, {}, function(data) {
$('body').append(data);
$('form[data-remote]').bind("ajax:beforeSend", function(e) {
console.log("Caught beforeSend!");
});
});
evt.preventDefault();
};
$(document).ready(function() {
$('a#signup').click(load_remote_form);
});
Chrome 的开发工具显示事件“ajax: beforeSend
”已绑定,但它从未被处理(当我发送表单时,我在javascript控制台中没有任何内容,尽管在rails日志中我看到请求已正确处理并且响应已发送)。我可以将其他事件绑定到同一个选择器 (form[data-remote]
),例如 click
,并且它们会被正确处理。
通过 .live
绑定的 Ajax 事件也不会得到处理。但是,如果表单作为布局的一部分呈现(即它位于独立页面上,例如 http://localhost:3000 /signup/),绑定的“ajax:beforeSend
”被正确处理。
我的控制器(只是为了一个案例,它可能写得不好,但我很确定它有效):
class UsersController < ApplicationController
layout :layout_for_type
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
session[:user_id] = @user.id
flash[:notice] = "You have successfully registered."
redirect_to root_url
else
if request.xhr?
render :action => 'new', :status => :unprocessable_entry, :layout => false
else
render :action => 'new'
end
end
end
def layout_for_type
request.xhr? ? nil : "application"
end
end
我做错了什么?可能有更好的方法来实现这种“双ajaxed”表单吗?
I'm trying to implement ajaxy signup with rails 3. I'm using jquery-ujs and remote form. I access the signup form with $.get
request, and it is displayed correctly. This signup form is remote:
form_for @user, :remote => true, :html => {"data-type" => "html"} do |f|
In my application.js, if the ajax request for getting the form is successful, I'm trying to bind handler for ajax events from unobtrusive javascript:
var load_remote_form = function(evt) {
var href = $(this).attr('href');
// load form template
$.get(href, {}, function(data) {
$('body').append(data);
$('form[data-remote]').bind("ajax:beforeSend", function(e) {
console.log("Caught beforeSend!");
});
});
evt.preventDefault();
};
$(document).ready(function() {
$('a#signup').click(load_remote_form);
});
Chrome's development tools show that the event "ajax:beforeSend
" is binded, but it is never handled (I have nothing in the javascript console when I send the form, though in rails log I see that the request is processed correctly and the response is send). I can bind other events to the same selector (form[data-remote]
), like click
, and they are handled correctly.
Ajax events binded through .live
are not handled as well. But if the form is rendered as part of the layout (i.e. it is on stand-alone page like http://localhost:3000/signup/), binded "ajax:beforeSend
" is handled correctly.
My controller (just for a case, it may be badly written, but I'm pretty sure it works):
class UsersController < ApplicationController
layout :layout_for_type
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
session[:user_id] = @user.id
flash[:notice] = "You have successfully registered."
redirect_to root_url
else
if request.xhr?
render :action => 'new', :status => :unprocessable_entry, :layout => false
else
render :action => 'new'
end
end
end
def layout_for_type
request.xhr? ? nil : "application"
end
end
What am I doing wrong? May be there is a better approach for implementing such "double-ajaxed" forms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
愚蠢的我。我完全忽略了另一个潜在的问题根源。 我的应用程序布局中有
,这导致将
jquery.js
和jquery.min.js
都包含到我的页面中。事实证明,这就是这种奇怪行为的原因。删除jquery.min.js
就可以了。希望有一天这对某人有用。Silly me. I've completely overlooked another potential source of problems. I had
in my application layout, and that caused including both
jquery.js
andjquery.min.js
into my page. It turned out to be the cause of that strange behavior. Removingjquery.min.js
did the trick. Hope that might be useful for someone someday.