From 351a224d90d5d485cb30743f1266ec2624ae7853 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Thu, 19 Oct 2006 22:03:27 +0000 Subject: Add support for converting blocks into function arguments in JavaScriptGenerator#call and JavaScriptProxy#call. Add JavaScriptGenerator#literal for wrapping a string in an object whose #to_json is the string itself. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5323 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/prototype_helper.rb | 47 ++++++++++++++++------ 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index f1b956cf12..1b4d2e71d8 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -458,6 +458,12 @@ module ActionView JavaScriptElementProxy.new(self, id) end + # Returns an object whose #to_json evaluates to +code+. Use this to pass a literal JavaScript + # expression as an argument to another JavaScriptGenerator method. + def literal(code) + JavaScriptLiteral.new(code) + end + # Returns a collection reference by finding it through a CSS +pattern+ in the DOM. This collection can then be # used for further method calls. Examples: # @@ -578,16 +584,18 @@ module ActionView call 'alert', message end - # Redirects the browser to the given +location+, in the same form as - # +url_for+. + # 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)})" + # Calls the JavaScript +function+, optionally with the given +arguments+. + # + # If a block is given, the block will be passed to a new JavaScriptGenerator; + # the resulting JavaScript code will then be wrapped inside function() { ... } + # and passed as the called function's final argument. + def call(function, *arguments, &block) + record "#{function}(#{arguments_for_call(arguments, block)})" end # Assigns the JavaScript +variable+ the given +value+. @@ -649,7 +657,7 @@ module ActionView end def record(line) - returning line = "#{line.to_s.chomp.gsub /\;$/, ''};" do + returning line = "#{line.to_s.chomp.gsub(/\;\z/, '')};" do self << line end end @@ -664,10 +672,16 @@ module ActionView object.respond_to?(:to_json) ? object.to_json : object.inspect end - def arguments_for_call(arguments) + def arguments_for_call(arguments, block = nil) + arguments << block_to_function(block) if block arguments.map { |argument| javascript_object_for(argument) }.join ', ' end + def block_to_function(block) + generator = self.class.new(@context, &block) + literal("function() { #{generator.to_s} }") + end + def method_missing(method, *arguments) JavaScriptProxy.new(self, method.to_s.camelize) end @@ -744,6 +758,13 @@ module ActionView end end + # Bypasses string escaping so you can pass around raw JavaScript + class JavaScriptLiteral < String #:nodoc: + def to_json + to_s + end + end + # Converts chained method calls on DOM proxy elements into JavaScript chains class JavaScriptProxy < Builder::BlankSlate #:nodoc: def initialize(generator, root = nil) @@ -752,16 +773,16 @@ module ActionView end private - def method_missing(method, *arguments) + def method_missing(method, *arguments, &block) if method.to_s =~ /(.*)=$/ assign($1, arguments.first) else - call("#{method.to_s.camelize(:lower)}", *arguments) + call("#{method.to_s.camelize(:lower)}", *arguments, &block) end end - def call(function, *arguments) - append_to_function_chain!("#{function}(#{@generator.send(:arguments_for_call, arguments)})") + def call(function, *arguments, &block) + append_to_function_chain!("#{function}(#{@generator.send(:arguments_for_call, arguments, block)})") self end @@ -770,7 +791,7 @@ module ActionView end def function_chain - @function_chain ||= @generator.instance_variable_get("@lines") + @function_chain ||= @generator.instance_variable_get(:@lines) end def append_to_function_chain!(call) -- cgit v1.2.3