【发布时间】:2014-06-04 04:14:04
【问题描述】:
我正在使用 Ruby on Rails 4,并且我有以下代码:
# view.html.erb
link_to("Title 1", url1, :remote => true, :method => :patch, :data => {:type => :html, :custom => true}
# <a rel="nofollow" href="/url1" data-type="html" data-custom="true" data-remote="true" data-method="patch">Title 1</a>
link_to("Title 2", url2, :remote => true, :method => :patch, :data => {:type => :html, :custom => true}
# <a rel="nofollow" href="/url2" data-type="html" data-custom="true" data-remote="true" data-method="patch">Title 2</a>
link_to("Title 3", url3, :remote => true, :method => :patch, :data => {:type => :html, :custom => true}
# <a rel="nofollow" href="/url3" data-type="html" data-custom="true" data-remote="true" data-method="patch">Title 3</a>
# application.js
$(document).ready(function() {
$('a[data-remote][data-custom]').bind({
click: function(event) {
$(document).on('ajax:success', function(xhr, data, status) {
$(this).replaceWith(data);
});
}
});
});
例如,当点击链接Title 1 时,成功时,响应/返回的数据为以下链接(点击链接Title 2 和Title 3 时的行为相同):
link_to("Replaced 1", url1, :remote => true, :method => :patch, :data => {:type => :html, :custom => true}
# <a rel="nofollow" href="/url1" data-type="html" data-custom="true" data-remote="true" data-method="patch">Replaced 1</a>
如您所见,响应/返回的链接与原始链接几乎相同(即,它具有 data-* 属性,就像原始点击的链接一样),因此 JavaScript 绑定应该在该链接之后发生它取代了 DOM。
但是,如果我继续像 Title 2 一样单击其他链接,那么前端内容中的 Title 1 和 Title 2 链接都会被替换,从而生成以下完整的 HTML 代码:
<a rel="nofollow" href="/url2" data-type="html" data-custom="true" data-remote="true" data-method="patch">Replaced 2</a>
<a rel="nofollow" href="/url2" data-type="html" data-custom="true" data-remote="true" data-method="patch">Replaced 2</a>
<a rel="nofollow" href="/url3" data-type="html" data-custom="true" data-remote="true" data-method="patch">Title 3</a>
为了完整起见,当我单击Title 3 时,会生成以下整个 HTML 代码:
<a rel="nofollow" href="/url3" data-type="html" data-custom="true" data-remote="true" data-method="patch">Replaced 3</a>
<a rel="nofollow" href="/url3" data-type="html" data-custom="true" data-remote="true" data-method="patch">Replaced 3</a>
<a rel="nofollow" href="/url3" data-type="html" data-custom="true" data-remote="true" data-method="patch">Replaced 3</a>
似乎 JavaScript 绑定没有按预期工作(注意重复的 Replaced <N> 链接)。我不明白为什么会发生这种情况,但预期的行为是每次单击该链接时只替换一个链接。
有什么问题?我该如何解决?
【问题讨论】:
-
你能添加代码来展示你的 AJAX 功能是如何工作的吗?
-
@awexfwex - 是的,我可以添加代码,但我不知道什么代码。
ruby-on-rails问题标签(您在编辑问题本身后删除)在那里,因为link_to(..., :remote => true, :method => :patch, ...是 a Rails AJAX functionality。 -
某处,您必须有一些 Javascript 代码执行实际的 ajax 调用。发布那个。 ruby 代码只是生成要使用的 html,对于这个问题似乎没有必要。
-
执行 AJAX 调用的代码在 rails.js 文件中的某处。
-
好的,有道理。我完全错了。我会更新我的答案。
标签: javascript jquery ruby-on-rails ajax html