Permalink
Browse files

Import periodically_call_remote

  • Loading branch information...
1 parent 7bf4ca4 commit 320caf591f6846b78e136392889a0860bd5b2330 @josh josh committed Jan 30, 2010
Showing with 43 additions and 0 deletions.
  1. +31 −0 lib/prototype_legacy_helper.rb
  2. +12 −0 test/test_prototype_legacy_helper.rb
@@ -92,6 +92,37 @@ def observe_form(form_id, options = {})
end
end
+ # Periodically calls the specified url (<tt>options[:url]</tt>) every
+ # <tt>options[:frequency]</tt> seconds (default is 10). Usually used to
+ # update a specified div (<tt>options[:update]</tt>) with the results
+ # of the remote call. The options for specifying the target with <tt>:url</tt>
+ # and defining callbacks is the same as link_to_remote.
+ # Examples:
+ # # Call get_averages and put its results in 'avg' every 10 seconds
+ # # Generates:
+ # # new PeriodicalExecuter(function() {new Ajax.Updater('avg', '/grades/get_averages',
+ # # {asynchronous:true, evalScripts:true})}, 10)
+ # periodically_call_remote(:url => { :action => 'get_averages' }, :update => 'avg')
+ #
+ # # Call invoice every 10 seconds with the id of the customer
+ # # If it succeeds, update the invoice DIV; if it fails, update the error DIV
+ # # Generates:
+ # # new PeriodicalExecuter(function() {new Ajax.Updater({success:'invoice',failure:'error'},
+ # # '/testing/invoice/16', {asynchronous:true, evalScripts:true})}, 10)
+ # periodically_call_remote(:url => { :action => 'invoice', :id => customer.id },
+ # :update => { :success => "invoice", :failure => "error" }
+ #
+ # # Call update every 20 seconds and update the new_block DIV
+ # # Generates:
+ # # new PeriodicalExecuter(function() {new Ajax.Updater('news_block', 'update', {asynchronous:true, evalScripts:true})}, 20)
+ # periodically_call_remote(:url => 'update', :frequency => '20', :update => 'news_block')
+ #
+ def periodically_call_remote(options = {})
+ frequency = options[:frequency] || 10 # every ten seconds by default
+ code = "new PeriodicalExecuter(function() {#{remote_function(options)}}, #{frequency})"
+ javascript_tag(code)
+ end
+
protected
def build_observer(klass, name, options = {})
if options[:with] && (options[:with] !~ /[\{=(.]/)
@@ -37,4 +37,16 @@ def test_observe_field_without_frequency
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.EventObserver('glass', function(element, value) {new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
observe_field("glass")
end
+
+ def test_periodically_call_remote
+ assert_dom_equal %(<script type="text/javascript">\n//<![CDATA[\nnew PeriodicalExecuter(function() {new Ajax.Updater('schremser_bier', 'http://www.example.com/mehr_bier', {asynchronous:true, evalScripts:true})}, 10)\n//]]>\n</script>),
+ periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" })
+ end
+
+ def test_periodically_call_remote_with_frequency
+ assert_dom_equal(
+ "<script type=\"text/javascript\">\n//<![CDATA[\nnew PeriodicalExecuter(function() {new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true})}, 2)\n//]]>\n</script>",
+ periodically_call_remote(:frequency => 2)
+ )
+ end
end

0 comments on commit 320caf5

Please sign in to comment.