mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
35b74de770
commit
c7f5a848f1
1 changed files with 31 additions and 11 deletions
|
@ -424,7 +424,7 @@ module ActionView
|
||||||
# page['blank_slate'].show # => $('blank_slate').show();
|
# page['blank_slate'].show # => $('blank_slate').show();
|
||||||
# page['blank_slate'].show('first').up # => $('blank_slate').show('first').up();
|
# page['blank_slate'].show('first').up # => $('blank_slate').show('first').up();
|
||||||
def [](id)
|
def [](id)
|
||||||
JavaScriptElementProxy.new(self, "$('#{id}')")
|
JavaScriptElementProxy.new(self, id)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a collection reference by finding it through a CSS +pattern+ in the DOM. This collection can then be
|
# 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 # => $$('p.welcome b').first();
|
||||||
# page.select('p.welcome b').first.hide # => $$('p.welcome b').first().hide();
|
# page.select('p.welcome b').first.hide # => $$('p.welcome b').first().hide();
|
||||||
def select(pattern)
|
def select(pattern)
|
||||||
JavaScriptElementProxy.new(self, "$$('#{pattern}')")
|
JavaScriptCollectionProxy.new(self, pattern)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Inserts HTML at the specified +position+ relative to the DOM element
|
# Inserts HTML at the specified +position+ relative to the DOM element
|
||||||
|
@ -666,20 +666,12 @@ module ActionView
|
||||||
end
|
end
|
||||||
|
|
||||||
# Converts chained method calls on DOM proxy elements into JavaScript chains
|
# 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)
|
def initialize(generator, root)
|
||||||
@generator = generator
|
@generator = generator
|
||||||
@generator << root
|
@generator << root
|
||||||
end
|
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
|
private
|
||||||
def method_missing(method, *arguments)
|
def method_missing(method, *arguments)
|
||||||
if method.to_s =~ /(.*)=$/
|
if method.to_s =~ /(.*)=$/
|
||||||
|
@ -707,5 +699,33 @@ module ActionView
|
||||||
function_chain[-1] += ".#{call};"
|
function_chain[-1] += ".#{call};"
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue