JavaScriptGenerator generates blocks of JavaScript code that allow you to change the content and presentation of multiple DOM elements. Use this in your Ajax response bodies, either in a <script> tag or as plain JavaScript sent with a Content-type of "text/javascript".

Create new instances with PrototypeHelper#update_page or with ActionController::Base#render, then call insert_html, replace_html, remove, show, hide, visual_effect, or any other of the built-in methods on the yielded generator in any order you like to modify the content and appearance of the current page.

Example:

  update_page do |page|
    page.insert_html :bottom, 'list', "<li>#{@item.name}</li>"
    page.visual_effect :highlight, 'list'
    page.hide 'status-indicator', 'cancel-link'
  end

generates the following JavaScript:

  new Insertion.Bottom("list", "<li>Some item</li>");
  new Effect.Highlight("list");
  ["status-indicator", "cancel-link"].each(Element.hide);

Helper methods can be used in conjunction with JavaScriptGenerator. When a helper method is called inside an update block on the page object, that method will also have access to a page object.

Example:

  module ApplicationHelper
    def update_time
      page.replace_html 'time', Time.now.to_s(:db)
      page.visual_effect :highlight, 'time'
    end
  end

  # Controller action
  def poll
    render(:update) { |page| page.update_time }
  end

You can also use PrototypeHelper#update_page_tag instead of PrototypeHelper#update_page to wrap the generated JavaScript in a <script> tag.

Methods
Public Instance methods
<<(javascript)

Writes raw JavaScript to the page.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 594
594:           def <<(javascript)
595:             @lines << javascript
596:           end
[](id)

Returns a element reference by finding it through id in the DOM. This element can then be used for further method calls. Examples:

  page['blank_slate']                  # => $('blank_slate');
  page['blank_slate'].show             # => $('blank_slate').show();
  page['blank_slate'].show('first').up # => $('blank_slate').show('first').up();
     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 452
452:           def [](id)
453:             JavaScriptElementProxy.new(self, id)
454:           end
alert(message)

Displays an alert dialog with the given message.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 572
572:           def alert(message)
573:             call 'alert', message
574:           end
assign(variable, value)

Assigns the JavaScript variable the given value.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 589
589:           def assign(variable, value)
590:             record "#{variable} = #{javascript_object_for(value)}"
591:           end
call(function, *arguments)

Calls the JavaScript function, optionally with the given arguments.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 584
584:           def call(function, *arguments)
585:             record "#{function}(#{arguments_for_call(arguments)})"
586:           end
delay(seconds = 1) {|| ...}

Executes the content of the block after a delay of seconds. Example:

  page.delay(20) do
    page.visual_effect :fade, 'notice'
  end
     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 603
603:           def delay(seconds = 1)
604:             record "setTimeout(function() {\n\n"
605:             yield
606:             record "}, #{(seconds * 1000).to_i})"
607:           end
draggable(id, options = {})

Creates a script.aculo.us draggable element. See ActionView::Helpers::ScriptaculousHelper for more information.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 625
625:           def draggable(id, options = {})
626:             record @context.send(:draggable_element_js, id, options)
627:           end
drop_receiving(id, options = {})

Creates a script.aculo.us drop receiving element. See ActionView::Helpers::ScriptaculousHelper for more information.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 631
631:           def drop_receiving(id, options = {})
632:             record @context.send(:drop_receiving_element_js, id, options)
633:           end
hide(*ids)

Hides the visible DOM elements with the given ids.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 562
562:           def hide(*ids)
563:             call 'Element.hide', *ids
564:           end
insert_html(position, id, *options_for_render)

Inserts HTML at the specified position relative to the DOM element identified by the given id.

position may be one of:

:top:HTML is inserted inside the element, before the element’s existing content.
:bottom:HTML is inserted inside the element, after the element’s existing content.
:before:HTML is inserted immediately preceeding the element.
:after:HTML is inserted immediately following the element.

options_for_render may be either a string of HTML to insert, or a hash of options to be passed to ActionView::Base#render. For example:

  # Insert the rendered 'navigation' partial just before the DOM
  # element with ID 'content'.
  insert_html :before, 'content', :partial => 'navigation'

  # Add a list item to the bottom of the <ul> with ID 'list'.
  insert_html :bottom, 'list', '<li>Last item</li>'
     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 503
503:           def insert_html(position, id, *options_for_render)
504:             insertion = position.to_s.camelize
505:             call "new Insertion.#{insertion}", id, render(*options_for_render)
506:           end
redirect_to(location)

Redirects the browser to the given location, in the same form as url_for.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 578
578:           def redirect_to(location)
579:             assign 'window.location.href', @context.url_for(location)
580:           end
remove(*ids)

Removes the DOM elements with the given ids from the page.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 552
552:           def remove(*ids)
553:             record "#{javascript_object_for(ids)}.each(Element.remove)"
554:           end
replace(id, *options_for_render)

Replaces the "outer HTML" (i.e., the entire element, not just its contents) of the DOM element with the given id.

options_for_render may be either a string of HTML to insert, or a hash of options to be passed to ActionView::Base#render. For example:

  # Replace the DOM element having ID 'person-45' with the
  # 'person' partial for the appropriate object.
  replace_html 'person-45', :partial => 'person', :object => @person

This allows the same partial that is used for the insert_html to be also used for the input to replace without resorting to the use of wrapper elements.

Examples:

  <div id="people">
    <%= render :partial => 'person', :collection => @people %>
  </div>

  # Insert a new person
  page.insert_html :bottom, :partial => 'person', :object => @person

  # Replace an existing person
  page.replace 'person_45', :partial => 'person', :object => @person
     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 547
547:           def replace(id, *options_for_render)
548:             call 'Element.replace', id, render(*options_for_render)
549:           end
replace_html(id, *options_for_render)

Replaces the inner HTML of the DOM element with the given id.

options_for_render may be either a string of HTML to insert, or a hash of options to be passed to ActionView::Base#render. For example:

  # Replace the HTML of the DOM element having ID 'person-45' with the
  # 'person' partial for the appropriate object.
  replace_html 'person-45', :partial => 'person', :object => @person
     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 517
517:           def replace_html(id, *options_for_render)
518:             call 'Element.update', id, render(*options_for_render)
519:           end
select(pattern)

Returns a collection reference by finding it through a CSS pattern in the DOM. This collection can then be used for further method calls. Examples:

  page.select('p')                      # => $$('p');
  page.select('p.welcome b').first      # => $$('p.welcome b').first();
  page.select('p.welcome b').first.hide # => $$('p.welcome b').first().hide();

You can also use prototype enumerations with the collection. Observe:

  page.select('#items li').each do |value|
    value.hide
  end
  # => $$('#items li').each(function(value) { value.hide(); });

Though you can call the block param anything you want, they are always rendered in the javascript as ‘value, index.’ Other enumerations, like collect() return the last statement:

  page.select('#items li').collect('hidden') do |item|
    item.hide
  end
  # => var hidden = $$('#items li').collect(function(value, index) { return value.hide(); });
     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 477
477:           def select(pattern)
478:             JavaScriptElementCollectionProxy.new(self, pattern)
479:           end
show(*ids)

Shows hidden DOM elements with the given ids.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 557
557:           def show(*ids)
558:             call 'Element.show', *ids
559:           end
sortable(id, options = {})

Creates a script.aculo.us sortable element. Useful to recreate sortable elements after items get added or deleted. See ActionView::Helpers::ScriptaculousHelper for more information.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 619
619:           def sortable(id, options = {})
620:             record @context.send(:sortable_element_js, id, options)
621:           end
toggle(*ids)

Toggles the visibility of the DOM elements with the given ids.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 567
567:           def toggle(*ids)
568:             call 'Element.toggle', *ids
569:           end
visual_effect(name, id = nil, options = {})

Starts a script.aculo.us visual effect. See ActionView::Helpers::ScriptaculousHelper for more information.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 611
611:           def visual_effect(name, id = nil, options = {})
612:             record @context.send(:visual_effect, name, id, options)
613:           end