views:

1223

answers:

2

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?

A: 

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)+'))'
tom
Thanks for the response but.. I think that you failed to answer the right question. My issue is with the page.call("buildYUITreeView", "project_tree", @current_project.get_directory_contents(@current_project.local_path, 0)) call in the RJS file not in the partial.
DL Redden
Ok, gotcha. Sorry I didn't read close enough. Editing the answer.
tom
+2  A: 

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.

ryanb