From c00487420ea973c596e7cd077fb1198b9da5ed92 Mon Sep 17 00:00:00 2001 From: Herval Freire Date: Sun, 22 Jun 2008 10:29:56 -0300 Subject: [PATCH] Added name support for periodically_call_remote in order to allow stopping it via javascript --- .../lib/action_view/helpers/prototype_helper.rb | 11 ++++++++++- actionpack/test/template/prototype_helper_test.rb | 5 +++++ 2 files changed, 15 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index a7c3b9d..12c7141 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -259,7 +259,9 @@ module ActionView # options[:frequency] seconds (default is 10). Usually used to # update a specified div (options[:update]) with the results # of the remote call. The options for specifying the target with :url - # and defining callbacks is the same as link_to_remote. + # and defining callbacks is the same as link_to_remote. You can also specify a :name, + # in case you need to manipulate the PeriodicalExecuter using javascript code (for example, if you need + # to turn the periodical execution off after some time) # Examples: # # Call get_averages and put its results in 'avg' every 10 seconds # # Generates: @@ -280,9 +282,16 @@ module ActionView # # new PeriodicalExecuter(function() {new Ajax.Updater('news_block', 'update', {asynchronous:true, evalScripts:true})}, 20) # periodically_call_remote(:url => 'update', :frequency => '20', :update => 'news_block') # + # # Call update every 10 seconds and update the new_block DIV - you can call 'my_updater.stop()' via javascript (or via an rjs call) + # # to stop the periodical execution, thanks to the :name parameter + # # Generates: + # # var my_updater = new PeriodicalExecuter(function() {new Ajax.Updater('news_block', 'update', {asynchronous:true, evalScripts:true})}, 20) + # periodically_call_remote(:url => 'update', :frequency => '20', :update => 'news_block', :name => 'my_updater') def periodically_call_remote(options = {}) frequency = options[:frequency] || 10 # every ten seconds by default + name = options[:name] # an optional name for the PeriodicalExecuter variable code = "new PeriodicalExecuter(function() {#{remote_function(options)}}, #{frequency})" + code = "var #{name} = #{code}" if name javascript_tag(code) end diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 60b83b4..161b13e 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -94,6 +94,11 @@ class PrototypeHelperTest < PrototypeHelperBaseTest periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) end + def test_periodically_call_remote_with_name + assert_dom_equal %(), + periodically_call_remote(:update => "schremser_bier", :name => "updater", :url => { :action => "mehr_bier" }) + end + def test_periodically_call_remote_with_frequency assert_dom_equal( "", -- 1.5.4.5