【发布时间】:2015-11-09 19:09:02
【问题描述】:
我的 Rails 应用程序中有一系列控制器,它们都在通过 Bootstrap 3 模态输入成功提交 AJAX 后呈现 create.js.erb 文件。我还按照this description here 添加了错误处理——所以当服务器返回一个无法处理的实体时,每个模式的错误都会在<span></span> 帮助块中呈现。所有这些都完美无缺。
我的问题是代码看起来不是很干。我添加到应用程序的每个新模态(我现在最多 7 个)都要求我在 Jquery/AJAX 错误处理中定位它(以我使用它的方式) - 请参见下面的代码:
$(document).ready(function(){
$(document).bind('ajaxError', 'form#new_person', function(event, jqxhr, settings, exception){
// note: jqxhr.responseJSON undefined, parsing responseText instead
$(event.data).render_form_errors( $.parseJSON(jqxhr.responseText) );
});
});
相关部分为$(document).bind('ajaxError', 'form#new_person', function(event, jqxhr, settings, exception){
这是针对form#new_person,我的应用程序中的每个表单都有一个。它工作正常,但似乎没有必要重复这么多,因为我在每个代码块中更改的只是选择器。
有没有一种方法可以添加到这里,我可以将每个选择器添加为列表,或者以某种方式告诉 Jquery 只触发一次 AJAXerror 调用?
【问题讨论】:
-
你想要什么?你想在特定的 ajax 调用而不是每次调用时调用它吗?
标签: javascript jquery ruby-on-rails ajax twitter-bootstrap