Rails 3:如何从 js.erb 文件调用 javascript 函数
现在我已经升级到 Rails 3,我正在尝试找出分离和重用 javascript 片段的正确方法。这是我正在处理的场景:
我有一个包含两个区域的页面:一个区域包含可拖动的元素,另一个区域包含可放置的元素。
当页面加载时,我使用 jQuery 来设置可拖动和可放置。目前我的脚本位于 application.html.erb 的头部,我确信这不是正确的解决方案,但至少有效。
当我按下页面上的按钮时,会对我的控制器进行 ajax 调用,将可拖动元素替换为一组也应该可拖动的新元素。我有一个 js.erb 文件,它在正确的位置呈现部分内容。渲染后,我需要使新元素可拖动,因此我想重用当前位于 application.html.erb 中的代码,但我还没有找到正确的方法。我只能通过将代码直接粘贴到我的 js.erb 文件中来使新元素可拖动(恶心)。
我想要什么: - 包含函数 prepdraggables() 和 prepdroppables() 的 javascript 文件 - 从 application.html.erb 或 js.erb 文件调用任一函数的方法
我尝试使用 :content_for 来存储和重用代码,但似乎无法使其正常工作。
的 head 部分中拥有什么
<% content_for :drag_drop_prep do %>
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
// declare all DOM elements with class draggable to be draggable
$( ".draggable" ).draggable( { revert : 'invalid' });
// declare all DOM elements with class legal to be droppable
$(".legal").droppable({
hoverClass : 'legal_hover',
drop : function(event, ui) {
var c = new Object();
c['die'] = ui.draggable.attr("id");
c['cell'] = $(this).attr("id");
c['authenticity_token'] = encodeURIComponent(window._token);
$.ajax({
type: "POST",
url: "/placeDie",
data: c,
timeout: 5000
});
}});
});
</script>
<% end %>
我目前在 application.html.erb undo.js.erb
$("#board").html("<%= escape_javascript(render :partial => 'shared/board', :locals => { :playable => true, :restartable => !session[:challenge]}) %>")
// This is where I want to prepare draggables.
<%= javascript_include_tag "customdragdrop.js" %> // assuming this file had the draggables code from above in a prepdraggables() function
prepdraggables();
Now that I've upgraded to Rails 3, I'm trying to figure out the proper way to separate and reuse pieces of javascript. Here's the scenario I'm dealing with:
I have a page with two areas: one with elements that should be draggable, the other with droppables.
When the page loads I use jQuery to setup the draggables and droppables. Currently I have the script in the head portion of application.html.erb, which I'm sure is not the right solution but at least works.
When I press a button on the page, an ajax call is made to my controller that replaces the draggables with a new set of elements that should also be draggable. I have a js.erb file that renders a partial in the correct location. After rendering I need to make the new elements draggable, so I'd like to reuse the code that currently lives in application.html.erb, but I haven't found the right way to do it. I can only make the new elements draggable by pasting the code directly into my js.erb file (yuck).
What I'd like to have:
- a javascript file that contains the functions prepdraggables() and prepdroppables()
- a way to call either function from application.html.erb or from a js.erb file
I've tried using :content_for to store and reuse the code, but can't seem to get it working correctly.
What I currently have in the head section of application.html.erb
<% content_for :drag_drop_prep do %>
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
// declare all DOM elements with class draggable to be draggable
$( ".draggable" ).draggable( { revert : 'invalid' });
// declare all DOM elements with class legal to be droppable
$(".legal").droppable({
hoverClass : 'legal_hover',
drop : function(event, ui) {
var c = new Object();
c['die'] = ui.draggable.attr("id");
c['cell'] = $(this).attr("id");
c['authenticity_token'] = encodeURIComponent(window._token);
$.ajax({
type: "POST",
url: "/placeDie",
data: c,
timeout: 5000
});
}});
});
</script>
<% end %>
undo.js.erb
$("#board").html("<%= escape_javascript(render :partial => 'shared/board', :locals => { :playable => true, :restartable => !session[:challenge]}) %>")
// This is where I want to prepare draggables.
<%= javascript_include_tag "customdragdrop.js" %> // assuming this file had the draggables code from above in a prepdraggables() function
prepdraggables();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道你不能将drag_drop_prep中的代码放入一个函数中,然后从application.html.erb和每个部分调用该函数吗?我想我误会了。
在 application.html.erb 和 undo.js.erb 中:
Couldn't you just put the code in drag_drop_prep into a function and then call the function from the application.html.erb and each partial? I'm guessing I have misunderstood.
And in application.html.erb and undo.js.erb: