Render Javascript / AJAX-Request

Hi there,

i just started using the yii framework, after working for some time with ruby on rails. from rails, i’m used to to do AJAX-Requests the following way: first, i set the html5 data-attribute (data-remote) of a form or a link to true. by doing that, an AJAX-Request is started when sending the form/clicking the link. In the controller, i do some database-stuff, e.g. saving data and/or calculating new values after the save. After that, i render a js.erb-file (Javascript with embedded Ruby-Code), where i can update several elements of my page with the new calculated values. I can do that, because all instance-variables from the controller are available in this file (it is handled like a ‘normal’ view-file).

Maybe i’m stuck in old behavior, and what i’m trying to do here isn’t the ‘Yii-way’ at all. I’ve already learnt that a usual Yii-AJAX-Request is done by using a ajaxSubmitButton, which then creates inline Javascript (what i don’t like in the first place…) and also accepts an update- or replace-Parameter where i name the css-element to be updated with the AJAX-Response, where the AJAX-Response is usually a (one!) Partial that i render in the controller. But what i’m missing here is the possibility to update different elements with different values at once.

So: Is it possible to render a Javascript-File in the controller? And if not, how can i return more than just one variable from the controller to the AJAX-Callback?

If someone could help me out here, i’d really appreciate it.

The "update" or "replace" attributes you are able to specify in ajaxSubmitButton are essentially shortcuts for

success: function(data){$("#updateMe").html(data)}

However, you can specify the success attribute in the ajax options of the submitButton with a function that can do anything you want with the returned data. The controller action doesn’t have to render a view, you could also echo some JSON that you can use to send complex data to the client. You could also render a view that contains pure javascript, that’s not a problem

Thanks a lot. This is some information that really helped me. I’ll test that and may get back to you if there are questions left.