HTML::Prototype - Generate HTML and Javascript for the Prototype library
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(...);
Some code generators for Prototype, the famous JavaScript OO library. This library allows you to do Ajax without writing lots of javascript code.
This is mostly a port of the Ruby on Rails helper tags for JavaScript for use in Catalyst.
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.
script/create.pl Prototype
script
Returns a form tag that will submit in the background using XMLHttpRequest, instead of the regular reloading POST arrangement.
Even though it's 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.
url
link_to_remote
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()' )
Returns a link to a remote action defined by options url that's 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}.
$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 asynchronously, during which various callbacks can be triggered (e.g. for progress indicators and the like).
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.
loading
loaded: Called when the browser has finished loading the remote document.
loaded
interactive: Called when the user can interact with the remote document, even though it has not finished loading.
interactive
complete: Called when the XMLHttpRequest is complete.
complete
If you do need synchronous processing (this will block the browser while the request is happening), you can specify $options->{type} = 'synchronous'.
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.
frequency
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.
update
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.
with
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' } ) %]
Like observe_field, but operates on an entire form identified by the DOM ID $id.
observe_field
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.
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.
Returns a button input tag that will submit a form using XMLHttpRequest in the background instead of a typical reloading via POST.
options argument is the same as in form_remote_tag
options
form_remote_tag
Catalyst::Plugin::Prototype, Catalyst. http://prototype.conio.net/
Sebastian Riedel, sri@oook.de Marcus Ramberg, mramberg@cpan.org
sri@oook.de
mramberg@cpan.org
Built around Prototype by Sam Stephenson. Much code is ported from Ruby on Rails javascript helpers.
Drew Taylor, Leon Brocard
This library is free software. You can redistribute it and/or modify it under the same terms as perl itself.
To install HTML::Prototype, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::Prototype
CPAN shell
perl -MCPAN -e shell install HTML::Prototype
For more information on module installation, please visit the detailed CPAN module installation guide.