关于不引人注目的 javascript 我不明白的一件事

发布于 2024-09-18 07:18:14 字数 804 浏览 15 评论 0原文

我喜欢分离功能的想法,这似乎是未来的发展方向。

但我习惯于在嵌入式语言(如 Rails ERB 或 PHP)中将 javascript 集成到循环内,在其中我可以使用特定对象的 ID 作为 javascript 中的引用。

Rails 示例:

<% @comments.each do |comment| %>
  <div id="comment_<%= comment.id %>">
    <%= comment.text %>
    <% link_to_function "Reply", "$('comment_#{comment.id}').insert(\"#{escape_javascript(render :partial => "_form", :locals => {:comment => comment})}\", {position: 'bottom'});" %>
  </div>
<% end %>

这也不是我唯一一次想在 javascript 中使用 Ruby 方法。我可能想使用常量,或者在循环 user.enabled?user.full_name 内的对象上调用其他 ruby​​ 方法,或者使用这些对象渲染部分内容等。

那么,如果所有 javascript 都在另一个文件中或在循环之外,这应该如何完成?我知道你可以使用 CSS 选择器在 javascript 中迭代一堆 div,但这不允许我在对象上调用 ruby​​ 方法。

我缺少什么?谢谢!

I like the idea of separating functionality and this seems like the way of the future.

But I'm used to integrating javascript inside loops in an embedded language like Rails ERB or PHP, where I can use the ID of a particular object as a reference in the javascript.

Rails example:

<% @comments.each do |comment| %>
  <div id="comment_<%= comment.id %>">
    <%= comment.text %>
    <% link_to_function "Reply", "$('comment_#{comment.id}').insert(\"#{escape_javascript(render :partial => "_form", :locals => {:comment => comment})}\", {position: 'bottom'});" %>
  </div>
<% end %>

This isn't the only time I've ended up wanting to use Ruby methods inside javascript either. I may want to use constants, or call other ruby methods on an object inside a loop user.enabled? or user.full_name, or render partials with those objects, etc.

So how is this supposed to be accomplished if all the javascript is in another file or outside the loop? I get that you can iterate through a bunch of divs in javascript using CSS selectors, but this doesn't allow me to call ruby methods on objects.

What am I missing? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜扑 2024-09-25 07:18:14

我认为应该使用“data-id”参数来完成,如本截屏视频所示
http://railscasts.com/episodes/229-polling-for-changes

I think it shold be done with "data-id" parameter as shown in this screencast
http://railscasts.com/episodes/229-polling-for-changes

醉酒的小男人 2024-09-25 07:18:14

对于您的特定示例,您已经在标记中编码了评论 ID,因为您将 div 元素的 ID 属性设置为评论 ID。所以你可以把 JavaScript 挂起来。

For your particular example you already have the comment ID encoded within the markup because you set the ID attribute of the div element to the comment ID. So you can hang the JavaScript off that.

耶耶耶 2024-09-25 07:18:14

请原谅我使用 jquery,但是如果没有它或类似的库,Web 开发真的很糟糕

对于你的第一个抱怨(获取当前评论),javascript this 对我来说工作得很好。

onclick="my_function(this);"

在js文件中

my_function = function(target) {
    // clicked element passed in
    // now, let's get comment element by class
    var comment = $(target).parents('.comment');
}

至于第二个抱怨...我从来不经常需要它,但有时我在html中包含一些数据以供javascript使用。扩展前面的示例:

<div class="comment" comment_id="<%= comment.id %>"></div>

my_function

var comment_id = comment.attr('comment_id');

Pardon me for using jquery, but web development really sucks without it or similar library

For your first complaint (get current comment), javascript this works fine for me.

onclick="my_function(this);"

and in js file

my_function = function(target) {
    // clicked element passed in
    // now, let's get comment element by class
    var comment = $(target).parents('.comment');
}

As for second complaint... I never needed it often, but sometimes I include pieces of data in html for use by javascript. Extending previous example:

<div class="comment" comment_id="<%= comment.id %>"></div>

And in my_function

var comment_id = comment.attr('comment_id');
清风疏影 2024-09-25 07:18:14

让您的客户端脚本采用“选项”对象作为参数。不要使用任何服务器端脚本;将脚本放入其自己的 .js 文件中。

然后使用输出 js 的服务器端脚本创建“选项”对象。包含您的脚本并调用其入口点函数,传入您的“选项”对象。

Make your client-side script take an 'options' object as a parameter. Don't use any server-side scripting; put the script in its own .js file.

Then create the 'options' object with a server-side script which outputs js. Include your script and call its entry-point function, passing in your 'options' object.

橘虞初梦 2024-09-25 07:18:14

您只需要更多的 JavaScript 知识即可看到它的强大功能,尤其是 jQuery。我会解决你的例子:

<% @comments.each do |comment| %>
  <div class="comment">
    <%= comment.text %>
    <a href="reply.php?id=<%= comment.id %>" class="reply">Reply</a>
  </div>
<% end %>
<script type="text/javascript">
  $('.comment .reply').click(function(e) {
    e.preventDefault();
    var url = $(this).attr('href');
    var result = url.match(/\?id=(\d+)([^\d]|$)/);
    var id = (result && result[1]);
    if (id) {
      // do whatever you want to do with the id
    }
  });
</script>

好处:

(1)你没有到处都有javascript

(2)无需javascript即可工作

You just need some more JavaScript knowledge to see power of it, especially jQuery. Your example I would solve like:

<% @comments.each do |comment| %>
  <div class="comment">
    <%= comment.text %>
    <a href="reply.php?id=<%= comment.id %>" class="reply">Reply</a>
  </div>
<% end %>
<script type="text/javascript">
  $('.comment .reply').click(function(e) {
    e.preventDefault();
    var url = $(this).attr('href');
    var result = url.match(/\?id=(\d+)([^\d]|$)/);
    var id = (result && result[1]);
    if (id) {
      // do whatever you want to do with the id
    }
  });
</script>

Benefits:

(1) You don't have javascript all over the place

(2) Works without javascript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文