NAME
HTML::Prototype - Generate HTML and Javascript for the Prototype library
SYNOPSIS
use HTML::Prototype;
my $prototype = HTML::Prototype->new;
print $prototype->define_javascript_functions;
print $prototype->form_remote_tag(...);
print $prototype->link_to_function(...);
print $prototype->link_to_remote(...);
print $prototype->observe_field(...);
print $prototype->observe_form(...);
print $prototype->periodically_call_remote(...);
print $prototype->submit_to_remote(...);
DESCRIPTION
Some code generators for Prototype, the famous JavaScript oo library.
This is mostly a port of the Ruby on Rails helper tags for JavaScript for use in Catalyst.
METHODS
$prototype->define_javascript_functions
Returns the library of JavaScript functions and objects, in a script block.
Notes for Catalyst users:
You can use script/create.pl Prototype
to generate a static JavaScript file which then can be included via remote script
tag.
$prototype->form_remote_tag(\%options)
Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular reloading POST arrangement.
Even though its using JavaScript to serialize the form elements, the form submission will work just like a regular submission as viewed by the receiving side.
The options for specifying the target with url
and defining callbacks is the same as link_to_remote
.
$prototype->link_to_function( $name, $function )
Returns a link that will trigger a JavaScript function using the onClick handler and return false after the fact.
Examples:
$prototype->link_to_function( "Greeting", "alert('Hello world!') )
$prototype->link_to_function( '<img src="really.png"/>', 'do_delete()' )
$prototype->link_to_remote( $id, \%options )
Returns a link to a remote action defined by options url
thats called in the background using XMLHttpRequest.
The result of that request can then be inserted into a DOM object whose id can be specified with options->{update}.
Examples:
$prototype->link_to_remote( 'Delete', {
update => 'posts',
url => 'http://localhost/posts/'
} )
$prototype->link_to_remote( '<img src="refresh.png"/>', {
update => 'emails',
url => 'http://localhost/refresh/'
} )
By default, these remote requests are processed asynchronous during which various callbacks can be triggered (for progress indicators and the likes).
Example:
$prototype->link_to_remote( 'count', {
url => 'http://localhost/count/',
complete => 'doStuff(request)'
} )
The callbacks that may be specified are:
loading
: Called when the remote document is being loaded with data by the browser.
loaded
: Called when the browser has finished loading the remote document.
interactive
: Called when the user can interact with the remote document, even though it has not finished loading.
complete
: Called when the XMLHttpRequest is complete.
If you for some reason or another need synchronous processing (that will block the browser while the request is happening), you can specify $options->{type} = 'synchronous'.
$prototype->observe_field( $id, \%options)
Observes the field with the DOM ID specified by $id and makes an Ajax when its contents have changed.
Required options are:
frequency
: The frequency (in seconds) at which changes to this field will be detected.
url
: url to be called when field content has changed.
Additional options are:
update
: Specifies the DOM ID of the element whose innerHTML should be updated with the XMLHttpRequest response text.
with
: A JavaScript expression specifying the parameters for the XMLHttpRequest. This defaults to value, which in the evaluated context refers to the new field value.
Additionally, you may specify any of the options documented in link_to_remote
.
Example TT2 template in Catalyst:
[% c.prototype.define_javascript_functions %]
<h1>[% page.title %]</h1>
<div id="view"></div>
<textarea id="editor" rows="24" cols="80">[% page.body %]</textarea>
[% url = base _ 'edit/' _ page.title %]
[% c.prototype.observe_field( 'editor', {
url => url,
with => "'body='+value",
update => 'view'
} ) %]
$prototype->observe_form( $id, \%options )
Like observe_field
, but operates on an entire form identified by the DOM ID $id.
Options are the same as observe_field
, except the default value of the with
option evaluates to the serialized (request string) value of the form.
$prototype->periodically_call_remote( \%options )
Periodically calls the specified url $options->{url} every $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
.
$prototype->submit_to_remote( $name, $value, \%options )
Returns a button input tag that will submit form using XMLHttpRequest in tghe background instead of regular reloading POST arrangement.
options
argument is the same as in form_remote_tag
SEE ALSO
Catalyst::Plugin::Prototype, Catalyst.
AUTHOR
Sebastian Riedel, sri@oook.de
Built around Prototype by Sam Stephenson. Much code is ported from Ruby on Rails javascript helpers.
THANK YOU
Drew Taylor
LICENSE
This library is free software . You can redistribute it and/or modify it under the same terms as perl itself.