关于不引人注目的 javascript 我不明白的一件事
我喜欢分离功能的想法,这似乎是未来的发展方向。
但我习惯于在嵌入式语言(如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为应该使用“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
对于您的特定示例,您已经在标记中编码了评论 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.请原谅我使用 jquery,但是如果没有它或类似的库,Web 开发真的很糟糕
对于你的第一个抱怨(获取当前评论),javascript
this
对我来说工作得很好。在js文件中
至于第二个抱怨...我从来不经常需要它,但有时我在html中包含一些数据以供javascript使用。扩展前面的示例:
在
my_function
中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.and in js file
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:
And in
my_function
让您的客户端脚本采用“选项”对象作为参数。不要使用任何服务器端脚本;将脚本放入其自己的 .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.
您只需要更多的 JavaScript 知识即可看到它的强大功能,尤其是 jQuery。我会解决你的例子:
好处:
(1)你没有到处都有javascript
(2)无需javascript即可工作
You just need some more JavaScript knowledge to see power of it, especially jQuery. Your example I would solve like:
Benefits:
(1) You don't have javascript all over the place
(2) Works without javascript