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
上级 35b74de7
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册