CGI-Application-Demo-Ajax

 view release on metacpan or  search on metacpan

lib/CGI/Application/Demo/Ajax.pm  view on Meta::CPAN


Point your web client at http://127.0.0.1/cgi-bin/ajax.cgi or http://127.0.0.1/local/ajax.

This is equivalent to C<< CGI::Application::Demo::Ajax -> new() -> run() >>.

Since there is no run mode input, the code defaults to Ajax.pm's sub initialize(). See sub setup() for details.

=item The code assembles the default web page

The work is done in Ajax.pm's sub initialize().

This page is sent from the server to the client.

It contains the contents of web.page.tmpl, with both search.js and search.tmpl embedded therein.

Of course, it also contains a minimal set of YUI Javascript files.

=item The client accepts the response

The default web page is displayed.

=item You input a search term

The C<CGI> form in search.tmpl is set to not submit, but rather to call the Javascript function search_onsubmit(),
which lives in search.js.

It's actually the copy of this code, now inside web.page.tmpl, now inside your client, which gets executed.

=item The C<CGI> form is submitted

Here, Javascript does the submit, in such a way as to also specify a call-back (Javascript) function, search_callback(),
which will handle the response from the server.

This function also lives in search.js.

=item Ajax.pm runs again

This time a run mode was submitted, either as form data or as path info data.

And this means that when using the fancy script, you don't need the line in search.tmp referring to the hidden form
variable 'rm', because of the path info '/search' in search_onsubmit().

=item sub search() carries out the search.

The run mode causes Ajax.pm's sub search() to be the sub which gets executed this time.

It assembles the results, and uses C<JSON::XS> to encode them.

=item The server replies

The results of the search are sent to the client.

=item The client accepts the response

When the client receives the message, these events occur, in this order:

=over 4

=item Control passes to search_callback(), the call-back function

=item The data is decoded from JSON to text by a YAHOO.lang.JSON object

=item The data is moved into a YAHOO.util.LocalDataSource object

=item The data is formatted as it's moved into a YAHOO.widget.DataTable object

=back

This object displays its data automatically. Actually, the object's constructor displays the data, which is why
we call new by assigning the object to a Javascript variable, data_table.

=back

=head2 Next

It should be obvious that the code in Ajax.pm's sub search() can be extended in any manner, to pass more complex
hash refs to the Javascript function search_callback().

This data can then be ignored by the Javascript, or you can extend the responseSchema and column_defs to display it.

Given this framework, extending these data structures is basically effortless.

=head1 Author

C<CGI::Application::Demo::Ajax> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2009.

Home page: http://savage.net.au/index.html

=head1 Copyright

Australian copyright (c) 2009, Ron Savage.
	All Programs of mine are 'OSI Certified Open Source Software';
	you can redistribute them and/or modify them under the terms of
	The Artistic License, a copy of which is available at:
	http://www.opensource.org/licenses/index.html

=cut



( run in 1.387 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )