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

时间:2022-10-27 09:50:32

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].

在Ruby on Rails项目中,我需要在Ajax调用结束时执行JavaScript函数。我的问题是传递给使用page.call调用的JavaScript函数的值正在用引号括起来。如果您传入字符串但其中一个值是JavaScript数组的字符串表示,即[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。

<%= 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

在project_controller中

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

And in change_project.rjs

在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.

是什么导致我的问题。我只是希望它发送值,例如[0,1,2,3],但它发送“[0,1,2,3]”,这导致JS爆炸。

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代码。

<% 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.

考虑到这一点,我正在使用部分并且只是在需要时将其渲染为调用JS函数,但这看起来像是一个黑客。

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?

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

2 个解决方案

#1


4  

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.

汤姆走在正确的轨道上,< <方法是要走的路,因为它会直接插入任何javascript。你会想要使用它而不是呼叫。< p>

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.

您可以使用YAHOO行执行相同的操作,因为tom显示哪个应该比使用带有javascript标记的partial更有效。

#2


1  

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

您可以使用< <方法来发出原始javascript:< p>

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

#1


4  

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.

汤姆走在正确的轨道上,< <方法是要走的路,因为它会直接插入任何javascript。你会想要使用它而不是呼叫。< p>

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.

您可以使用YAHOO行执行相同的操作,因为tom显示哪个应该比使用带有javascript标记的partial更有效。

#2


1  

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

您可以使用< <方法来发出原始javascript:< p>

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