[ANN] IFRAME based RJS - responds_to_parent

** File uploads with AJAX mojo **

Respond with RJS to your parent window with a form action targeted to
a hidden IFRAME. Handles all the painful situations like scoping
your JavaScript to your parent window generating the script block for
execution and clearing the IFRAME after execution so the back button
doesn’t re-execute the action.

plugin install http://sean.treadway.info/svn/plugins/ responds_to_parent

class EnterpriseElevatorPitchExampleController < ActionController::Base
def iframe_target_action
responds_to_parent do
render :update do |page|
page.hide :progress_bar
page.show :success
page.replace_html :success, ‘Finished’
page.alert “RJS isn’t limited to XMLHttpRequest”
end
end
end
end

Discuss, comment, and feedback for improvement:
http://sean.treadway.info/articles/2006/05/29/iframe-remoting-made-easy

RespondsToParent

Adds responds_to_parent to your controller to respond to the parent
document of your page.
Make Ajaxy file uploads by posting the form to a hidden iframe, and
respond with
RJS to the parent window.

Example

Controller:
class Test < ActionController::Base
def main
end

 def form_action
   # Do stuff with params[:uploaded_file]

   responds_to_parent do
     render :update do |page|
       page << "alert($('stuff').innerHTML)"
     end
   end
 end

end

main.rhtml:

Here is some stuff

Published at http://agilewebdevelopment.com/plugins/
responds_to_parent :slight_smile:


Benjamin C.
http://www.bencurtis.com/
http://www.tesly.com/ – Collaborative test case management
http://www.agilewebdevelopment.com/ – Resources for the Rails community

Sean Treadway wrote:

** File uploads with AJAX mojo **

Respond with RJS to your parent window with a form action targeted to
a hidden IFRAME. Handles all the painful situations like scoping
your JavaScript to your parent window generating the script block for
execution and clearing the IFRAME after execution so the back button
doesn’t re-execute the action.

plugin install http://sean.treadway.info/svn/plugins/ responds_to_parent

class EnterpriseElevatorPitchExampleController < ActionController::Base
def iframe_target_action
responds_to_parent do
render :update do |page|
page.hide :progress_bar
page.show :success
page.replace_html :success, ‘Finished’
page.alert “RJS isn’t limited to XMLHttpRequest”
end
end
end
end

Discuss, comment, and feedback for improvement:
http://sean.treadway.info/articles/2006/05/29/iframe-remoting-made-easy

RespondsToParent

Adds responds_to_parent to your controller to respond to the parent
document of your page.
Make Ajaxy file uploads by posting the form to a hidden iframe, and
respond with
RJS to the parent window.

Example

Controller:
class Test < ActionController::Base
def main
end

 def form_action
   # Do stuff with params[:uploaded_file]

   responds_to_parent do
     render :update do |page|
       page << "alert($('stuff').innerHTML)"
     end
   end
 end

end

main.rhtml:

Here is some stuff

Sean,
Thanks for this excellent solution. I’ve successfully deployed this
with my app.

But there is one small annoyance that I’ve encountered and was
wondering if others are experiencing the same:

I’m running Firefox on Mac (and linux) with Firebug installed. After I
upload an image, I get the following javascript error in Firebug:

#–
loc is not defined
[Break on this error] with(window.parent) { setTimeout(function() {
window.eval('try {\nElemen…
#–

I know it’s small, and does come with any side-effects, but still, it is
annoying. Any ideas?

Elliott,

I had the same firebug error. For me the issue was that my response RJS
was replacing the iframe I had posted to. Maybe you are having the same
problem?

I am happy to help if you want to post your code or something…

Thanks
-Jon