diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-02-14 04:39:44 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-02-14 04:39:44 +0000 |
commit | c7f5a848f107bf294302b7eca606930168cc9e98 (patch) | |
tree | f322e9641f0e8e1576cbe3c083c1e2acda0612aa /actionpack | |
parent | 35b74de7703515cf93da2d3e850702f58a2a6f48 (diff) | |
download | rails-c7f5a848f107bf294302b7eca606930168cc9e98.tar.gz rails-c7f5a848f107bf294302b7eca606930168cc9e98.tar.bz2 rails-c7f5a848f107bf294302b7eca606930168cc9e98.zip |
Refactor the Javascript proxy into a hierarchy for element/collection [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3592 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/prototype_helper.rb | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 9a65fee855..8a4498ae89 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -424,7 +424,7 @@ module ActionView # page['blank_slate'].show # => $('blank_slate').show(); # page['blank_slate'].show('first').up # => $('blank_slate').show('first').up(); def [](id) - JavaScriptElementProxy.new(self, "$('#{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 @@ -434,7 +434,7 @@ module ActionView # 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}')") + JavaScriptCollectionProxy.new(self, pattern) end # Inserts HTML at the specified +position+ relative to the DOM element @@ -666,20 +666,12 @@ module ActionView end # Converts chained method calls on DOM proxy elements into JavaScript chains - class JavaScriptElementProxy < Builder::BlankSlate #:nodoc: + class JavaScriptProxy < Builder::BlankSlate #:nodoc: def initialize(generator, root) @generator = generator @generator << root end - def replace_html(*options_for_render) - call 'update', @generator.render(*options_for_render) - end - - def replace(*options_for_render) - call 'replace', @generator.render(*options_for_render) - end - private def method_missing(method, *arguments) if method.to_s =~ /(.*)=$/ @@ -707,5 +699,33 @@ module ActionView function_chain[-1] += ".#{call};" end end + + class JavaScriptElementProxy < JavaScriptProxy #:nodoc: + def initialize(generator, id) + @id = id + super(generator, "$('#{id}')") + end + + def replace_html(*options_for_render) + call 'update', @generator.send(:render, *options_for_render) + end + + def replace(*options_for_render) + call 'replace', @generator.send(:render, *options_for_render) + end + + def reload + replace :partial => @id.to_s + end + end + + class JavaScriptCollectionProxy < JavaScriptProxy #:nodoc: + def initialize(generator, pattern) + @pattern = pattern + super(generator, "$$('#{pattern}')") + end + + # TODO: Implement funky stuff like .each + end end end |