将对象从 Rails RJS 传递到 javascript 函数调用而不引用值?

发布于 2024-07-29 06:06:55 字数 1540 浏览 6 评论 0原文

在 Ruby on Rails 项目中,我需要在 Ajax 调用结束时执行 JavaScript 函数。 我的问题是传递到使用 page.call 调用的 JavaScript 函数的值被引号括起来。 如果您传入字符串,但其中一个值是 JavaScript 数组的字符串表示形式,即 [0,1,2,3],那么问题不大。

以下是我认为重要的代码片段。

我有一个观察选择下拉列表的观察字段。

<%= observe_field("project_select_list",
    :loading => "Element.show('tree_indicator')",
    :complete => "Element.hide('tree_indicator')",
    :url => {:controller => "projects", :action => "change_project"},
    :with => "id")
%>

在project_controller

def change_project()
    @current_project = Project.find(params[:id])
end

和change_project.rjs 中

page.replace_html("project_name", @current_project.name)
page.visual_effect(:highlight, "project_name")
page.call("buildYUITreeView", "project_tree", @current_project.get_directory_contents(@current_project.local_path, 0))

最后一个值:

@current_project.get_directory_contents(@current_project.local_path, 0))

是导致我出现问题的原因。 我只是希望它发送值,例如 [0,1,2,3],但它发送“[0,1,2,3]”,这导致 JS 崩溃。

该部分完成了它应该做的事情,即它将数据而不是字符串发送到放在页面上的 JavaScript 代码。

<% javascript_tag do -%>
    YAHOO.util.Event.addListener("dom:loaded", "load", buildYUITreeView("<%= tree_id %>", <%= project.get_directory_contents(project.local_path, 0) %>));
<% end -%>

考虑到这一点,我正在尝试使用部分并在需要时渲染它以调用 JS 函数,但这看起来像是一个 hack。

如何使 page.call 不将作为参数发送的数据用引号括起来,或者如何在 Ajax 调用完成后将此数据传递给 JS 函数以执行?

In a Ruby on Rails project I need to have a JavaScript function executed at the end of an Ajax call. My problem is that the values being passed into the JavaScript function being called with page.call are being wrapped up in quotes. Not too much of a problem if you're passing in strings but one of the values is a string representation of a JavaScript Array, i.e. [0,1,2,3].

Here are what I feel are the important snippets of code.

I have an observe_field that watches a select drop down list.

<%= observe_field("project_select_list",
    :loading => "Element.show('tree_indicator')",
    :complete => "Element.hide('tree_indicator')",
    :url => {:controller => "projects", :action => "change_project"},
    :with => "id")
%>

In the project_controller

def change_project()
    @current_project = Project.find(params[:id])
end

And in change_project.rjs

page.replace_html("project_name", @current_project.name)
page.visual_effect(:highlight, "project_name")
page.call("buildYUITreeView", "project_tree", @current_project.get_directory_contents(@current_project.local_path, 0))

The last value:

@current_project.get_directory_contents(@current_project.local_path, 0))

is what's causing me issues. I just want it to send the value, for example [0,1,2,3], but it's sending "[0,1,2,3]" which is causing the JS to blow up.

This partial does what it's supposed to in that it sends the data and not a string to the JavaScript code that gets put on the page.

<% javascript_tag do -%>
    YAHOO.util.Event.addListener("dom:loaded", "load", buildYUITreeView("<%= tree_id %>", <%= project.get_directory_contents(project.local_path, 0) %>));
<% end -%>

With this in mind I'm playing around with using a partial and just rendering it to call the JS function when I need to but that seems like such a hack.

How can I make page.call not wrap the data sent as parameters in quotes, or how should I go about passing this data to the JS function for execution after the Ajax call is complete?

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

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

发布评论

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

评论(2

世界等同你 2024-08-05 06:06:55

Tom 的思路是正确的,<< 方法是正确的选择,因为它会直接插入任何 javascript。 您需要使用它来代替 call

page << "buildUYITreeView('project_tree', #{@current_project.get_directory_contents(@current_project.local_path, 0))})"

您可以使用 Tom 所示的 YAHOO 行执行相同的操作,这应该比使用带有 javascript 标记的部分更有效。

Tom was on the right track, the << method is the way to go because it will insert any javascript directly. You'll want to use that instead of call.

page << "buildUYITreeView('project_tree', #{@current_project.get_directory_contents(@current_project.local_path, 0))})"

You can do the same thing with the YAHOO line as tom showed which should be more efficient than using a partial with a javascript tag.

夜还是长夜 2024-08-05 06:06:55

您可以使用 << 方法来代替发出原始 JavaScript:

page << 'YAHOO.util.Event.addListener("dom:loaded", "load", buildYUITreeView("project_tree", '+@current_project.get_directory_contents(@current_project.local_path, 0)+'))'

You could use the << method instead to emit raw javascript:

page << 'YAHOO.util.Event.addListener("dom:loaded", "load", buildYUITreeView("project_tree", '+@current_project.get_directory_contents(@current_project.local_path, 0)+'))'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文