Ruby on rails Rails 3,引导模式多层远程调用
我已经为此绞尽脑汁两天了。我非常沮丧,而且我似乎无法通过搜索找到任何关于这方面的信息 这个问题。我使用:remote=>true链接从不同的控制器加载一些htmlRuby on rails Rails 3,引导模式多层远程调用,ruby-on-rails,ruby-on-rails-3,modal-dialog,twitter-bootstrap,Ruby On Rails,Ruby On Rails 3,Modal Dialog,Twitter Bootstrap,我已经为此绞尽脑汁两天了。我非常沮丧,而且我似乎无法通过搜索找到任何关于这方面的信息 这个问题。我使用:remote=>true链接从不同的控制器加载一些html $('.managed_locations').bind('ajax:complete', function(evt, xhr, status){ $('#locations_modal').modal('show') $('#locations_modal').html(xhr.responseText); });
$('.managed_locations').bind('ajax:complete', function(evt, xhr, status){
$('#locations_modal').modal('show')
$('#locations_modal').html(xhr.responseText);
});
因此,它获取html,将其转储到引导模式并显示模式。这很好用
但在模态内部,我还有一个表单也使用:remote=>true。现在为了让生活更艰难,当按下按钮时,我克隆表单并显示它。因此,用户可以有许多表单
现在是问题。无论何时提交表单,它都会像普通页面一样加载表单。就好像:remote=>true被忽略了一样。但这只是在模态中。如果我自己加载模态控制器,它就可以正常工作。在使用另一个jquery lightbox之前,我也开发了这个工具,它工作得很好。我只是为了一致性而切换引导
所以我最初的想法是jquery_ujs.js没有找到新的表单。所以我添加了一些代码来输出表单元素
$("#log_events").click(function () {
$(document).find(".new_stored_physical_location").each(function() {
console.log( $(this).data() );
console.log( $(this).data('events') );
});
return false;
});
控制台中的哪些输出:
Object { type="html", remote=true}
Object { ajax:complete=[1]}
所以我看到事件是在jQuery中设置的。这些表单中的每一个都有:remote=>true,并且在请求完成时都有ajax事件。但当我点击submit时,它并没有执行ajax请求
我是否缺少一些东西来确保ajax请求从表单中发生???数据()看起来很好,数据('events')看起来很好。但是,我是否还需要查看其他事件/绑定
现在从模式加载的html正在加载布局。但我做这两件事都有布局,没有布局。它快把我逼疯了。谢谢你们的帮助
编辑:一些额外的古怪。模态还加载一些额外的远程链接,所有这些链接都正常工作。只有表单链接似乎不起作用。我找到了一个解决方案。jquery_ujs.js中存在一个大问题,尤其是这一行:
$(document).delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
仅供参考,rails.formSubmitSelector='form'。所以这段代码找到了文档中的所有表单,用这个函数重写了提交。但问题是,一旦加载了一些ajax,并且ajax包含了一个事件,它就不会向其中添加这个奇特的事件。您需要重新添加它
这就是我所做的
在jquery_ujs内部有一系列函数,可以使用$.rails在其外部访问。比如:$.rails.enableElement,$.rails.nonBlankInputs。提交事件的代码被随意搁置。它仅在加载页面时执行一次。所以我把它放在函数addSubmitEvent()中:
当我多次打开/关闭模式时,我就遇到了添加事件次数过多的问题。所以我只在它周围放了一个简单的真/假if,只叫它一次
// Add the form submit event
addSubmitEvent: function(element) {
//$(element) was before $(document) but I changed it
$(element).delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
var form = $(this),
remote = form.data('remote') !== undefined,
blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
if (!rails.allowAction(form)) return rails.stopEverything(e);
// skip other logic when required values are missing or file upload is present
if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
return rails.stopEverything(e);
}
if (remote) {
if (nonBlankFileInputs) {
return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
}
// If browser does not support submit bubbling, then this live-binding will be called before direct
// bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
if (!$.support.submitBubbles && $().jquery < '1.7' && rails.callFormSubmitBindings(form, e) === false) return rails.stopEverything(e);
rails.handleRemote(form);
return false;
} else {
// slight timeout so that the submit button gets properly serialized
setTimeout(function(){ rails.disableFormElements(form); }, 13);
}
});
}
$.rails.addSubmitEvent('#my_modal');