Catalyst-View-TT-Progressive

 view release on metacpan or  search on metacpan

lib/Catalyst/View/TT/Progressive.pm  view on Meta::CPAN

				var wrapper = document.querySelector('[progressive]');
				wrapper.innerHTML = res;
			},
			error: function (req) {}
		};

		window.addEventListener('click', function (event) {
			if (event.target.tagName === 'A') {
				event.preventDefault();
				progressive.request(event.target.href, progressive.render, progressive.error);
			}
		});

		document.querySelectorAll('.menu-link').forEach(function (link) {
			link.addEventListener('click', function (event) {
				progressive.menu.querySelector('.menu-selected').classList.remove('menu-selected');
				event.target.parentNode.classList.add('menu-selected');
			});
		});

		...

	})();


=== Alternative app.js when handling a json response ===

	$c->stash(JSON => {
		abc => 1
	});
 
	(function () {
		var progressive = {	
			menu: document.getElementById('menu'),
			request: function (url, cb, ecb) {
				if (url.match('^#')) return;
				var request = new XMLHttpRequest();
				request.onreadystatechange = function () {
					if (request.readyState === XMLHttpRequest.DONE) request.status === 200 
						? cb(JSON.parse(request.response)) 
						: ecb(request); 
				};
				request.open('GET', url);
				request.setRequestHeader('api', 1);
				request.setRequestHeader('WRAPPER', 'new_wrapper.tt');
				request.send();
			},
			render: function (res) {
				var wrapper = document.querySelector('[progressive]');
				wrapper.innerHTML = res.html;
				if (res.abc) {
					... instantiate some JS ...
				}
			},
			error: function (req) {}
		};

		...
	})();

when using this approach you may want to prevent accessing of endpoints directly from a browser an easy way of achieving this is checking for a header in Root->auto and then redirecting.

        unless ($c->req->header('api')) {
                $c->response->redirect($c->uri_for('/invalid_url'));
                return;
        }

=head1 AUTHOR

LNATION, C<< <thisusedtobeanemail at gmail.com> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-catalyst-view-tt-progressive at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-TT-Progressive>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Catalyst::View::TT::Progressive

You can also look for information at:

=over 2

=item * RT: CPAN's request tracker (report bugs here)

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-View-TT-Progressive>

=item * Search CPAN

L<http://search.cpan.org/dist/Catalyst-View-TT-Progressive/>

=back

=head1 ACKNOWLEDGEMENTS


=head1 LICENSE AND COPYRIGHT

Copyright 2019->2025 LNATION.

This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:

L<http://www.perlfoundation.org/artistic_license_2_0>

Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.



( run in 2.193 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )