在没有 Rails 的情况下使用 AJAX 的最佳实践 内置助手?
在我的博客应用程序中,一些帖子显示为摘录,即用户看到前(比如说)500 个字符,并且可以单击链接来查看整个帖子。 以下是相关部分:
<% href = url_for post_path(:id => post) %>
<h1 class="title"><%= post.title %></h1>
<h2 class="published_on"><%= post.author %> wrote this <%= time_ago_in_words(post.published_on)%> ago</h2>
<div class="body">
<% if defined?(length) %>
<%= truncate_html(post.body, :length => length, :omission => "…<h1><a class='more' href=\"#{href}\">Click here for more!</a></h1>") %>
<% else %>
<%= post.body %>
<% end %>
</div>
但是,而不是“单击此处了解更多!” 将用户带到一个单独的页面,我希望它能够内嵌帖子的其余部分。 目前,我一直在通过将上面的代码片段放入以下 div 中来实现这一点:
<div class="post" id="post_<%= post.id %>">
<%= render :partial => 'post_content', :locals => { :post => post, :length => 500 } %>
</div>
然后我在 application.js 中使用此 div 的 id 来执行 AJAX:
$(document).ready(function() {
$("a.more").click(function() {
var url = $(this).attr('href');
var id = url.split("/")[2]
$.get(url, null, function(data) {
$("#post_" + id).html(data);
});
return false;
});
});
这显然很恶心 - 我不希望我的 javascript取决于链接的 href 中帖子 id 的位置,但我不知道 javascript 是否有任何其他方式可以知道它正在获取哪个帖子,因此应该将内容插入到哪个 div 中。
实现这一目标的最佳方法是什么? 我应该继续使用 Rails 的 AJAX 助手吗?
In my blog application, some posts appear as excerpts -- i.e., the user sees the first (say) 500 characters, and can click a link to view the entire post. Here is the relevant partial:
<% href = url_for post_path(:id => post) %>
<h1 class="title"><%= post.title %></h1>
<h2 class="published_on"><%= post.author %> wrote this <%= time_ago_in_words(post.published_on)%> ago</h2>
<div class="body">
<% if defined?(length) %>
<%= truncate_html(post.body, :length => length, :omission => "…<h1><a class='more' href=\"#{href}\">Click here for more!</a></h1>") %>
<% else %>
<%= post.body %>
<% end %>
</div>
However, instead of "Click here for more!" taking the user to a separate page, I'd like it to populate the rest of the post inline. Currently, I've been implementing this by putting the above snippet in the following div:
<div class="post" id="post_<%= post.id %>">
<%= render :partial => 'post_content', :locals => { :post => post, :length => 500 } %>
</div>
I then use the id of this div in my application.js to do the AJAX:
$(document).ready(function() {
$("a.more").click(function() {
var url = $(this).attr('href');
var id = url.split("/")[2]
$.get(url, null, function(data) {
$("#post_" + id).html(data);
});
return false;
});
});
This is obviously disgusting -- I don't want my javascript to depend on the location of the post's id in the link's href, but I don't know any other way for the javascript to know which post it is getting and therefore into which div the content should be inserted.
What's the best way to accomplish this? Should I just go back to using rails' AJAX helpers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
霍拉斯,你是对的。 您根本不需要 Rails 助手。 为了实现不显眼的 JavaScript 必杀技,您最好避免使用它们(对不起 Marc!)
不显眼的 JS 意味着尽可能避免嵌入 JS。 事实上,尝试使事物保持松散耦合,但不要完全解耦。 在您给我们的示例中,这很简单,因为我们有一个来自 HREF 的 URL 可以使用。 你的 JS 不需要“知道”任何关于如何请求的事情。
以下是如何从 HREF 盲目发送链接,并让 Rails 使用 ajax 响应(即无布局)进行响应。
解决方案:
这还假设您有 RESTful 路由或类似的路由来处理 /post/:id
我们就完成了! 我相信这对我们所有人都有好处。 :D
Horace, you are correct. You don't need the Rails helpers at all. To achieve unobtrusive JavaScript Nirvana, you will do well to avoid them (sorry Marc!)
Unobstrusive JS means avoiding embedded JS where possible. Indeed, try to keep things loosely coupled, but not entirely decoupled. In the example you gave us, it is easy, because we have a URL from the HREF to work with. Your JS does not need to "know" anything about how to request.
Here is how to send through the link from the HREF's blindly, and get Rails to respond with an ajax response (i.e. no layout).
SOLUTION:
This also assumes you have RESTful routes or similar to handle /post/:id
And we're done! I believe there is something in that for all of us. :D
在这种情况下,我认为您根本不应该进行 AJAX 调用。 您已经在 post.body 中拥有原始帖子的全部内容(您没有从数据库中仅获取 500 个字符),而不是使用 JQuery 进行 ajax 调用,而是使用它来隐藏超过 500 个字符的内容,然后当用户单击“更多”链接,它会显示 div 的其余部分。 您可以完全在客户端完成此操作,从而节省了 ajax。
在你的部分:
你的JQuery:
In this case, I don't think you should have the AJAX call at all. You already have the original post's entire content in post.body (you didn't fetch just the 500 characters from the db) Instead of using JQuery to make an ajax call, use it to hide the content past 500 characters and then when the user clicks the 'more' link it shows the rest of the div. You can do this entirely on the client side and save yourself the ajax.
In your partial:
Your JQuery:
我认为对于这类问题你应该使用rails的助手。
HTML 会像这样:
更多链接会是:
控制器会做类似的事情:
另一种同样好的方法是加载所有内容然后隐藏它:
(div 标签上的 onclick 可能会在 IE 中中断,但是这个是给你这个想法)
对我来说,这将是最好的方法,除非你有充分的理由避免使用 Rails 的助手。
希望有帮助
In my opinion for this kind of problems you should use rails' helpers.
HTML would go like :
The more link would be :
And the controller would do something like :
The other way that could be just as good is to load everything and then hide it :
(the onclick on a div tag might break in IE but this is to give you the idea)
To me that would be the best way to go unless you have a good reason of avoiding rails' helpers.
Hope that helps