Permalink
Browse files

Added delayed execution of Javascript from within RJS (closes #3264) …

…[devslashnull@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3335 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Dec 22, 2005
1 parent 778d670 commit 4e76ae39047b369a192ee1a55cfcb4418c5c1f93
@@ -485,10 +485,21 @@ def assign(variable, value)
def <<(javascript)
@lines << javascript
end
-
+
+ # Executes the content of the block after a delay of +seconds+. Example:
+ #
+ # page.delay(20) do
+ # page.visual_effect :fade, 'notice'
+ # end
+ def delay(seconds = 1)
+ record "setTimeout(function() {\n\n"
+ yield
+ record "}, #{(seconds * 1000).to_i})"
+ end
+
private
def method_missing(method, *arguments, &block)
- record @context.send(method, *arguments, &block)
+ record(@context.send(method, *arguments, &block))
end
def record(line)
@@ -201,6 +201,14 @@ def test_redirect_to
@generator.redirect_to(:action => 'welcome')
end
+ def test_delay
+ @generator.delay(20) do
+ @generator.hide('foo')
+ end
+
+ assert_equal "setTimeout(function() {\n;\nElement.hide(\"foo\");\n}, 20000);", @generator.to_s
+ end
+
def test_to_s
@generator.insert_html(:top, 'element', '<p>This is a test</p>')
@generator.insert_html(:bottom, 'element', '<p>This is a test</p>')

0 comments on commit 4e76ae3

Please sign in to comment.