aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/prototype_helper.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-02-12 18:48:08 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-02-12 18:48:08 +0000
commitebdb766e7956520e9e5a784e280a8f1916f50511 (patch)
tree14749af9395b225cc609e4b1e12fcb84723f942a /actionpack/lib/action_view/helpers/prototype_helper.rb
parenta32a9863640dbd7a74b251e5467f90d134396110 (diff)
downloadrails-ebdb766e7956520e9e5a784e280a8f1916f50511.tar.gz
rails-ebdb766e7956520e9e5a784e280a8f1916f50511.tar.bz2
rails-ebdb766e7956520e9e5a784e280a8f1916f50511.zip
Added element and collection proxies to RJS [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3587 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers/prototype_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/prototype_helper.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb
index 26d877c00a..f0d2b405d2 100644
--- a/actionpack/lib/action_view/helpers/prototype_helper.rb
+++ b/actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -416,6 +416,26 @@ module ActionView
def to_s #:nodoc:
@lines * $/
end
+
+ # Returns a element reference by finding it through +id+ in the DOM. This element can then be
+ # used for further method calls. Examples:
+ #
+ # page['blank_slate'] # => $('blank_slate');
+ # page['blank_slate'].show # => $('blank_slate').show();
+ # page['blank_slate'].show('first').up # => $('blank_slate').show('first').up();
+ def [](id)
+ JavaScriptElementProxy.new(self, "$('#{id}')")
+ 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:
+ #
+ # page.select('p') # => $$('p');
+ # page.select('p.welcome b').first # => $$('p.welcome b').first();
+ # page.select('p.welcome b').first.hide # => $$('p.welcome b').first().hide();
+ def select(pattern)
+ JavaScriptElementProxy.new(self, "$$('#{pattern}')")
+ end
# Inserts HTML at the specified +position+ relative to the DOM element
# identified by the given +id+.
@@ -644,5 +664,24 @@ module ActionView
callbacks
end
end
+
+ # Converts chained method calls on DOM proxy elements into JavaScript chains
+ class JavaScriptElementProxy < Builder::BlankSlate #:nodoc:
+ def initialize(generator, root)
+ @generator = generator
+ @generator << root
+ end
+
+ private
+ def method_missing(method, *arguments)
+ method_chain = @generator.instance_variable_get("@lines")
+
+ last_method = method_chain[-1]
+ method_chain[-1] = last_method[0..-2] if last_method[-1..-1] == ";" # strip trailing ; from last method call
+ method_chain[-1] += ".#{method}(#{@generator.send(:arguments_for_call, arguments)});"
+
+ self
+ end
+ end
end
end