package WebGUI::Asset::HelloWorld; use Moose; use WebGUI::Definition::Asset; extends 'WebGUI::Asset'; define 'assetName' => 'Hello World'; sub www_view { my ( $self ) = @_; my $resp = $self->session->response; $resp->content_type('text/plain'); return "Hello, World!\n"; } 1;
use WebGUI::Definition::Asset; define 'assetName' => 'Hello World';
define 'tableName' => 'HelloWorld';
property 'greeting' => ( tab => 'properties', fieldType => 'text', label => 'Greeting', default => 'Hello', ); $self->greeting; $self->greeting( 'Bonjour' );
has 'something' => ( is => 'rw', isa => 'Array', );
$session->request->content_type( 'text/html' );
// etc/site.conf { "assets" : { "WebGUI::Asset::HelloWorld" : { "category" : "basic" }, ... } }
# plackup
package WebGUI::Asset::Wobject::SchemaManager; use Moose; use WebGUI::Definition::Asset; extends 'WebGUI::Asset::Wobject'; define 'assetName' => 'Schema Manager'; use My::Schema; has 'schema' => ( is => 'ro', lazy => 1, default => sub { my $self = $_[0]; My::Schema->connect( sub{ $self->session->db->dbh }, { unsafe => 1 } ); }, );
has 'resultClass' => ( is => 'ro', default => 'My::Schema::Result::Employee', ); has 'rs' => ( is => 'ro', lazy => 1, default => sub { $_[0]->schema->resultset( $_[0]->resultClass ) }, );
package My::Schema; use base 'DBIx::Class::Schema'; __PACKAGE__->load_namespaces(); package My::Schema::Result::Employee; use base 'DBIx::Class::Core'; __PACKAGE__->table('employee'); __PACKAGE__->add_columns(qw/ id name salary notes /); __PACKAGE__->set_primary_key('id');
mysql> select * from employee; +----+--------------+-------------------+--------------------------------------+ | id | name | salary | notes | +----+--------------+-------------------+--------------------------------------+ | 1 | Doug Bell | A pittance | Barely worth the damage he causes | | 2 | Chris Hanson | Nothing | Knows and uses VB. | | 3 | Graham Knop | Less than nothing | Knows and uses VB. Doesn't enjoy it. | +----+--------------+-------------------+--------------------------------------+ 3 rows in set (0.00 sec)
[% FOREACH row IN rows %] ([% row.id %]) [% row.name %] makes [% row.salary %]<br/> [% row.notes %]<br/> [% END %]
package WebGUI::Asset::Wobject::SchemaManager; # ... sub www_view { my ( $self ) = @_; my $session = $self->session; my $rs = $self->rs; my $template = WebGUI::Asset->newById( $session, 'TMPL_ID' ); $template->setParam( rows => [ map { { $_->get_columns } } $rs->all ] ); return $template; }
[% USE Macro %] [% Macro.AssetProxy( 'ASSET_ID' ) %]
[% INCLUDE template:/template/url %] [% INSERT asset:ASSET_ID %]
# etc/site.conf { "template" : { "INCLUDE_PATH" : "/data/WebGUI/tmpl", "PLUGIN_BASE" : "My::Template::Plugin" } }
{ foo => { bar => "baz" }
{ "foo.bar" => "baz" }
use WebGUI::FormBuilder; sub getRowForm { my ( $self, $row ) = @_; my $fb = WebGUI::FormBuilder->new( $self->session ); $fb->addField( "ReadOnly", name => 'id', label => 'ID', value => $row ? $row->id : $self->session->id->generate, ); $fb->addField( "Text", name => 'name', label => 'Name', value => $row ? $row->name : '', );
$fb->addField( "Text", name => 'salary', label => 'Salary', value => $row ? $row->salary : '', ); $fb->addField( "Textarea", name => 'notes', label => 'Notes', value => $row ? $row->notes : '', ); return $fb; }
$fb->getField( "notes" ) ->set( "label" => "Secret!" );
$template->setParam( %{ $fb->toTemplateVars } );
my $values = $fb->process;
if ( $fb->errors ) { return; }
sub www_editRow { my ( $self ) = @_; my $ses = $self->session; my $row; if ( my $id = $ses->request->param('id') ) { $row = $self->rs->find({ id => $id }); } my $fb = $self->getRowForm( $row ); $fb->addField( 'Hidden', name => 'func', value => 'editRowSave', ); $fb->addField( 'Submit', name => 'Save' ); return $fb->toHtml; }
sub www_editRowSave { my ( $self ) = @_; my $req = $self->session->request; my $values = $self->getRowForm->process; my $row = $self->rs->update_or_create( $values ); $self->session->response->setRedirect( $self->getUrl ); return; }
override dispatch => sub { my ( $self, $frag ) = @_; if ( $frag eq '.json' ) { return JSON->new->encode( [ map { { $_->get_columns } } $self->rs->all ] ); } super(); };
[ { "notes":"Knows and uses VB. Doesn't enjoy it.", "name":"Graham Knop", "id":"3", "salary":"Less than nothing" }, { "notes":"Knows and users VB", "name":"Chris Hanson", "id":"2", "salary":"Nothing" },{ "notes":"Barely worth the damage he causes.", "name":"Doug Bell", "id":"1", "salary":"A pittance" } ]
Broken in lots of ways!
Slides are licensed under a CC-BY-SA 3.0 license.
Code is licensed under the Artistic License or GNU GPL v1.0 or later, at your discretion (the same terms as Perl 5 itself).