diff options
author | Sam Stephenson <sam@37signals.com> | 2005-12-20 00:20:33 +0000 |
---|---|---|
committer | Sam Stephenson <sam@37signals.com> | 2005-12-20 00:20:33 +0000 |
commit | c61d27d0c730992d3e69e18d3a8b4e7a4dfeb56b (patch) | |
tree | 12ac0a8bd50803954dba94938e1ae679c11791ee /actionpack/lib | |
parent | 6b38b9739ac190505641e4637c79b3ff20950bb2 (diff) | |
download | rails-c61d27d0c730992d3e69e18d3a8b4e7a4dfeb56b.tar.gz rails-c61d27d0c730992d3e69e18d3a8b4e7a4dfeb56b.tar.bz2 rails-c61d27d0c730992d3e69e18d3a8b4e7a4dfeb56b.zip |
Add additional RJS functionality: alert, redirect_to, call, assign, and <<
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3319 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_view/helpers/prototype_helper.rb | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index a0f3a6b42d..89d9ea7f2d 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -422,8 +422,8 @@ module ActionView # insert_html :bottom, 'list', '<li>Last item</li>' # def insert_html(position, id, *options_for_render) - html = render(*options_for_render) - record "new Insertion.#{position.to_s.camelize}(#{id.inspect}, #{html.inspect})" + insertion = position.to_s.camelize + call "new Insertion.#{insertion}", id, render(*options_for_render) end # Replaces the inner HTML of the DOM element with the given +id+. @@ -436,25 +436,51 @@ module ActionView # replace_html 'person-45', :partial => 'person', :object => @person # def replace_html(id, *options_for_render) - html = render(*options_for_render) - record "Element.update(#{id.inspect}, #{html.inspect})" + call 'Element.update', id, render(*options_for_render) end # Removes the DOM elements with the given +ids+ from the page. def remove(*ids) - record "#{ids.inspect}.each(Element.remove)" + record "#{javascript_object_for(ids)}.each(Element.remove)" end # Shows hidden DOM elements with the given +ids+. def show(*ids) - record "Element.show(#{ids.map {|id| id.inspect} * ', '})" + call 'Element.show', *ids end # Hides the visible DOM elements with the given +ids+. def hide(*ids) - record "Element.hide(#{ids.map {|id| id.inspect} * ', '})" + call 'Element.hide', *ids end - + + # Displays an alert dialog with the given +message+. + def alert(message) + call 'alert', message + end + + # Redirects the browser to the given +location+, in the same form as + # +url_for+. + def redirect_to(location) + assign 'window.location.href', @context.url_for(location) + end + + # Calls the JavaScript +function+, optionally with the given + # +arguments+. + def call(function, *arguments) + record "#{function}(#{arguments_for_call(arguments)})" + end + + # Assigns the JavaScript +variable+ the given +value+. + def assign(variable, value) + record "#{variable} = #{javascript_object_for(value)}" + end + + # Writes raw JavaScript to the page. + def <<(javascript) + @lines << javascript + end + private def method_missing(method, *arguments, &block) record @context.send(method, *arguments, &block) @@ -462,7 +488,7 @@ module ActionView def record(line) returning line = "#{line.to_s.chomp.gsub /\;$/, ''};" do - @lines << line + self << line end end @@ -471,6 +497,14 @@ module ActionView @context.render(*options_for_render) : options_for_render.first.to_s end + + def javascript_object_for(object) + object.respond_to?(:to_json) ? object.to_json : object.inspect + end + + def arguments_for_call(arguments) + arguments.map { |argument| javascript_object_for(argument) }.join ', ' + end end # Yields a JavaScriptGenerator and returns the generated JavaScript code. |