Permalink
Browse files

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
  • Loading branch information...
1 parent 35b74de commit c7f5a848f107bf294302b7eca606930168cc9e98 @dhh dhh committed Feb 14, 2006
Showing with 31 additions and 11 deletions.
  1. +31 −11 actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -424,7 +424,7 @@ def to_s #:nodoc:
# 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 @@ def [](id)
# 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 @@ def build_callbacks(options)
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 @@ def append_to_function_chain!(call)
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

0 comments on commit c7f5a84

Please sign in to comment.