Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 1561

Show
Ignore:
Timestamp:
06/29/05 08:09:00 (3 years ago)
Author:
david
Message:

Fix JavaScriptHelper capitalization and make it compatible with Prototype #1545 [Sam Stephenson]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_view/helpers/javascript_helper.rb

    r1552 r1561  
    33module ActionView 
    44  module Helpers 
    5     # Provides a set of helpers for calling Javascript functions and, most importantly, to call remote methods using what has  
     5    # Provides a set of helpers for calling JavaScript functions and, most importantly, to call remote methods using what has  
    66    # been labelled Ajax[http://www.adaptivepath.com/publications/essays/archives/000385.php]. This means that you can call  
    77    # actions in your controllers without reloading the page, but still update certain parts of it using injections into the  
    88    # DOM. The common use case is having a form that adds a new element to a list without reloading the page. 
    99    # 
    10     # To be able to use the Javascript helpers, you must either call <tt><%= define_javascript_functions %></tt> (which returns all 
    11     # the Javascript support functions in a <script> block) or reference the Javascript library using  
     10    # To be able to use the JavaScript helpers, you must either call <tt><%= define_javascript_functions %></tt> (which returns all 
     11    # the JavaScript support functions in a <script> block) or reference the JavaScript library using  
    1212    # <tt><%= javascript_include_tag "prototype" %></tt> (which looks for the library in /javascripts/prototype.js). The latter is 
    1313    # recommended as the browser can then cache the library instead of fetching all the functions anew on every request. 
     
    1515    # If you're the visual type, there's an Ajax movie[http://www.rubyonrails.com/media/video/rails-ajax.mov] demonstrating 
    1616    # the use of form_remote_tag. 
    17     module JavascriptHelper       
     17    module JavaScriptHelper       
    1818      unless const_defined? :CALLBACKS 
    1919        CALLBACKS       =  
     
    102102      # 
    103103      # You can customize further browser side call logic by passing 
    104       # in Javascript code snippets via some optional parameters. In 
     104      # in JavaScript code snippets via some optional parameters. In 
    105105      # their order of use these are: 
    106106      # 
     
    127127 
    128128      # Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular  
    129       # reloading POST arrangement. Even though it's using Javascript to serialize the form elements, the form submission  
     129      # reloading POST arrangement. Even though it's using JavaScript to serialize the form elements, the form submission  
    130130      # will work just like a regular submission as viewed by the receiving side (all elements available in @params). 
    131131      # The options for specifying the target with :url and defining callbacks is the same as link_to_remote. 
    132132      # 
    133       # A "fall-through" target for browsers that doesn't do Javascript can be specified with the :action/:method options on :html 
     133      # A "fall-through" target for browsers that doesn't do JavaScript can be specified with the :action/:method options on :html 
    134134      # 
    135135      #   form_remote_tag :html => { :action => url_for(:controller => "some", :action => "place") } 
     
    187187      end 
    188188 
    189       # Includes the Action Pack Javascript libraries inside a single <script>  
     189      # Includes the Action Pack JavaScript libraries inside a single <script>  
    190190      # tag. The function first includes prototype.js and then its core extensions, 
    191191      # (determined by filenames starting with "prototype"). 
     
    227227      #                       innerHTML should be updated with the 
    228228      #                       XMLHttpRequest response text. 
    229       # <tt>:with</tt>::      A Javascript expression specifying the 
     229      # <tt>:with</tt>::      A JavaScript expression specifying the 
    230230      #                       parameters for the XMLHttpRequest. This defaults 
    231231      #                       to 'value', which in the evaluated context  
     
    271271      #                       entries returned by the Ajax request.  
    272272      #                       Defaults to field_id + '_auto_complete' 
    273       # <tt>:with</tt>::      A Javascript expression specifying the 
     273      # <tt>:with</tt>::      A JavaScript expression specifying the 
    274274      #                       parameters for the XMLHttpRequest. This defaults 
    275275      #                       to 'value', which in the evaluated context  
     
    352352      end 
    353353 
    354       # Escape carrier returns and single and double quotes for Javascript segments. 
     354      # Escape carrier returns and single and double quotes for JavaScript segments. 
    355355      def escape_javascript(javascript) 
    356356        (javascript || '').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" } 
    357357      end 
    358358 
    359       # Returns a Javascript tag with the +content+ inside. Example: 
     359      # Returns a JavaScript tag with the +content+ inside. Example: 
    360360      #   javascript_tag "alert('All is good')" # => <script type="text/javascript">alert('All is good')</script> 
    361361      def javascript_tag(content) 
     
    374374        js_options['method']       = method_option_to_s(options[:method]) if options[:method] 
    375375        js_options['insertion']    = "Insertion.#{options[:position].to_s.camelize}" if options[:position] 
    376         js_options['script']       = options[:script] == true if options[:script] 
     376        js_options['evalScripts']  = options[:script] == true if options[:script] 
    377377         
    378378        if options[:form] 
     
    438438      end 
    439439    end 
     440     
     441    JavascriptHelper = JavaScriptHelper unless const_defined? :JavascriptHelper 
    440442  end 
    441443end 
  • trunk/actionpack/README

    r1129 r1561  
    169169 
    170170 
    171 * Javascript and Ajax integration. 
     171* JavaScript and Ajax integration. 
    172172 
    173173    link_to_function "Greeting", "alert('Hello world!')" 
     
    175175                   :url => { :action => "destroy", :id => post.id } 
    176176   
    177   {Learn more}[link:classes/ActionView/Helpers/JavascriptHelper.html] 
     177  {Learn more}[link:classes/ActionView/Helpers/JavaScriptHelper.html] 
    178178 
    179179 
  • trunk/actionpack/test/template/javascript_helper.rb

    r1522 r1561  
    11require File.dirname(__FILE__) + '/../abstract_unit' 
    22 
    3 class JavascriptHelperTest < Test::Unit::TestCase 
    4   include ActionView::Helpers::JavascriptHelper 
     3class JavaScriptHelperTest < Test::Unit::TestCase 
     4  include ActionView::Helpers::JavaScriptHelper 
    55   
    66  include ActionView::Helpers::UrlHelper 
     
    2020  def test_define_javascript_functions 
    2121    # check if prototype.js is included first 
    22     assert_not_nil define_javascript_functions.split("\n")[1].match(/Prototype: an object-oriented Javascript library/) 
     22    assert_not_nil define_javascript_functions.split("\n")[1].match(/Prototype JavaScript framework/) 
    2323  end 
    2424 
  • trunk/actionpack/test/template/upload_progress_helper_test.rb

    r1553 r1561  
    4040  include ActionView::Helpers::TagHelper 
    4141  include ActionView::Helpers::UrlHelper 
    42   include ActionView::Helpers::JavascriptHelper 
     42  include ActionView::Helpers::JavaScriptHelper 
    4343  include ActionView::Helpers::UploadProgressHelper 
    4444 
     
    176176  include ActionView::Helpers::TagHelper 
    177177  include ActionView::Helpers::UrlHelper 
    178   include ActionView::Helpers::JavascriptHelper 
     178  include ActionView::Helpers::JavaScriptHelper 
    179179  include ActionView::Helpers::UploadProgressHelper 
    180180 
     
    259259  def test_form_tag_with_upload_progress 
    260260    assert_equal( 
    261       "<form action=\"http://www.example.com\" enctype=\"multipart/form-data\" method=\"post\" onsubmit=\"if (this.action.indexOf('upload_id') &lt; 0){ this.action += '?upload_id=1'; }this.target = 'UploadTarget1';$('UploadStatus1').innerHTML='Upload starting...'; $('UploadProgressBar1').firstChild.firstChild.style.width='0%'; if (document.uploadStatus1) { document.uploadStatus1.stop(); }document.uploadStatus1 = new Ajax.PeriodicalUpdater('UploadStatus1','http://www.example.com', {script:true, onComplete:function(request){$('UploadStatus1').innerHTML='A message';$('UploadProgressBar1').firstChild.firstChild.style.width='100%';document.uploadStatus1 = null}, asynchronous:true}.extend({decay:1.8,freqency:2.0})); return true\"><iframe id=\"UploadTarget1\" name=\"UploadTarget1\" src=\"\" style=\"width:0px;height:0px;border:0\"></iframe>", 
     261      "<form action=\"http://www.example.com\" enctype=\"multipart/form-data\" method=\"post\" onsubmit=\"if (this.action.indexOf('upload_id') &lt; 0){ this.action += '?upload_id=1'; }this.target = 'UploadTarget1';$('UploadStatus1').innerHTML='Upload starting...'; $('UploadProgressBar1').firstChild.firstChild.style.width='0%'; if (document.uploadStatus1) { document.uploadStatus1.stop(); }document.uploadStatus1 = new Ajax.PeriodicalUpdater('UploadStatus1','http://www.example.com', {onComplete:function(request){$('UploadStatus1').innerHTML='A message';$('UploadProgressBar1').firstChild.firstChild.style.width='100%';document.uploadStatus1 = null}, evalScripts:true, asynchronous:true}.extend({decay:1.8,freqency:2.0})); return true\"><iframe id=\"UploadTarget1\" name=\"UploadTarget1\" src=\"\" style=\"width:0px;height:0px;border:0\"></iframe>", 
    262262      form_tag_with_upload_progress 
    263263    ) 
     
    266266  def test_form_tag_with_upload_progress_custom 
    267267    assert_equal( 
    268       "<form action=\"http://www.example.com\" enctype=\"multipart/form-data\" method=\"post\" onsubmit=\"if (this.action.indexOf('upload_id') &lt; 0){ this.action += '?upload_id=5'; }this.target = 'awindow';$('UploadStatus0').innerHTML='Upload starting...'; $('UploadProgressBar0').firstChild.firstChild.style.width='0%'; alert('foo'); if (document.uploadStatus0) { document.uploadStatus0.stop(); }document.uploadStatus0 = new Ajax.PeriodicalUpdater('UploadStatus0','http://www.example.com', {script:true, onComplete:function(request){$('UploadStatus0').innerHTML='A message';$('UploadProgressBar0').firstChild.firstChild.style.width='100%';document.uploadStatus0 = null; alert('bar')}, asynchronous:true}.extend({decay:7,freqency:6})); return true\" target=\"awindow\">", 
     268      "<form action=\"http://www.example.com\" enctype=\"multipart/form-data\" method=\"post\" onsubmit=\"if (this.action.indexOf('upload_id') &lt; 0){ this.action += '?upload_id=5'; }this.target = 'awindow';$('UploadStatus0').innerHTML='Upload starting...'; $('UploadProgressBar0').firstChild.firstChild.style.width='0%'; alert('foo'); if (document.uploadStatus0) { document.uploadStatus0.stop(); }document.uploadStatus0 = new Ajax.PeriodicalUpdater('UploadStatus0','http://www.example.com', {onComplete:function(request){$('UploadStatus0').innerHTML='A message';$('UploadProgressBar0').firstChild.firstChild.style.width='100%';document.uploadStatus0 = null; alert('bar')}, evalScripts:true, asynchronous:true}.extend({decay:7,freqency:6})); return true\" target=\"awindow\">", 
    269269      form_tag_with_upload_progress({:upload_id => 5}, {:begin => "alert('foo')", :finish => "alert('bar')", :frequency => 6, :decay => 7, :target => 'awindow'})  
    270270    )