| 1 | # | ||
| 2 | # This file is part of CatalystX-ExtJS | ||
| 3 | # | ||
| 4 | # This software is Copyright (c) 2011 by Moritz Onken. | ||
| 5 | # | ||
| 6 | # This is free software, licensed under: | ||
| 7 | # | ||
| 8 | # The (three-clause) BSD License | ||
| 9 | # | ||
| 10 | package CatalystX::ExtJS::Tutorial; | ||
| 11 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | $CatalystX::ExtJS::Tutorial::VERSION = '2.1.3'; | |
| 13 | } | ||
| 14 | #ABSTRACT: Introduction to CatalystX::ExtJS | ||
| 15 | 1 | 1; | |
| 16 | |||
| 17 | |||
| 18 | =pod | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | CatalystX::ExtJS::Tutorial - Introduction to CatalystX::ExtJS | ||
| 23 | |||
| 24 | =head1 VERSION | ||
| 25 | |||
| 26 | version 2.1.3 | ||
| 27 | |||
| 28 | =head1 INTRODUCTION | ||
| 29 | |||
| 30 | =head1 TUTORIALS | ||
| 31 | |||
| 32 | L<CatalystX::ExtJS::Tutorial::Direct> | ||
| 33 | |||
| 34 | =head1 FIRST STEPS | ||
| 35 | |||
| 36 | These tasks are referenced from the tutorials above. | ||
| 37 | |||
| 38 | =head2 Step 1: Bootstrap Catalyst | ||
| 39 | |||
| 40 | In order to run the examples we need to bootstrap a Catalyst | ||
| 41 | application. | ||
| 42 | |||
| 43 | First go to your working directory and run: | ||
| 44 | |||
| 45 | # catalyst.pl MyApp | ||
| 46 | |||
| 47 | This will create a basic Catalyst application. Open up C<lib/MyApp.pm> | ||
| 48 | and add C<Unicode> to the list of plugins (after C<Static::Simple>). | ||
| 49 | |||
| 50 | =head2 Step 2: Add the Template View | ||
| 51 | |||
| 52 | Next we need a view. We will go with a L<Template::Alloy> view which will | ||
| 53 | take care of rendering the HTML and JavaScript sources. | ||
| 54 | Create C<lib/MyApp/View/TT.pm> with: | ||
| 55 | |||
| 56 | package MyApp::View::TT; | ||
| 57 | use Moose; | ||
| 58 | extends 'Catalyst::View::TT::Alloy'; | ||
| 59 | |||
| 60 | __PACKAGE__->config( { | ||
| 61 | CATALYST_VAR => 'c', | ||
| 62 | INCLUDE_PATH => [ MyApp->path_to( 'root', 'src' ) ] | ||
| 63 | } ); | ||
| 64 | 1; | ||
| 65 | |||
| 66 | =head2 Step 3: Adjust the Root Controller | ||
| 67 | |||
| 68 | The JavaScript sources should be generated through the view we just | ||
| 69 | created. For this to work, we need a controller, which handles that. | ||
| 70 | We can use the C<Root> controller which was created when | ||
| 71 | we created C<MyApp>. Open up C<lib/MyApp/Controller/Root.pm> and change | ||
| 72 | the C<index> subroutine to: | ||
| 73 | |||
| 74 | sub index :Path :Args(0) { } | ||
| 75 | |||
| 76 | This removes the Catalyst welcome message and a request to C</> will run | ||
| 77 | the C<index> template (which we will create later) via the TT view. | ||
| 78 | |||
| 79 | =head2 Step 4: Add the C<index> Template | ||
| 80 | |||
| 81 | Now it's time to build some HTML and JavaScript. Add this to C<root/src/index>: | ||
| 82 | |||
| 83 | <html> | ||
| 84 | <head> | ||
| 85 | <title>Ext.Direct and Catalyst</title> | ||
| 86 | <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-3.3.1/resources/css/ext-all.css" /> | ||
| 87 | <script type="text/javascript" src="http://extjs.cachefly.net/ext-3.3.1/adapter/ext/ext-base.js"></script> | ||
| 88 | <script type="text/javascript" src="http://extjs.cachefly.net/ext-3.3.1/ext-all-debug.js"></script> | ||
| 89 | <script type="text/javascript" src="/api/src"></script> | ||
| 90 | </head> | ||
| 91 | <body>Hello World!</body> | ||
| 92 | </html> | ||
| 93 | |||
| 94 | =head2 Step 5: Add the Direct API Controller | ||
| 95 | |||
| 96 | To have access to the API we need to add a new controller. Create | ||
| 97 | C<lib/MyApp/Controller/API.pm> and paste: | ||
| 98 | |||
| 99 | package MyApp::Controller::API; | ||
| 100 | use Moose; | ||
| 101 | extends q(CatalystX::Controller::ExtJS::Direct::API); | ||
| 102 | 1; | ||
| 103 | |||
| 104 | Now we create an action which will route any request to C</js/*> to | ||
| 105 | the according template in C<root/src/js>. | ||
| 106 | |||
| 107 | sub js : Path : Args { | ||
| 108 | my ($self, $c, $template) = @_; | ||
| 109 | $c->stash->{template} = $template; | ||
| 110 | } | ||
| 111 | |||
| 112 | =head2 Step 6: Add a DBIC Model | ||
| 113 | |||
| 114 | To play around with actual data, we need to set up a model. | ||
| 115 | We will be using L<DBIx::Class> as ORM which means we have to | ||
| 116 | set up a DBIC schema first. | ||
| 117 | |||
| 118 | Create the file C<lib/MyApp/Schema.pm> and paste the following: | ||
| 119 | |||
| 120 | package MyApp::Schema; | ||
| 121 | use Moose; | ||
| 122 | extends 'DBIx::Class::Schema'; | ||
| 123 | __PACKAGE__->load_namespaces; | ||
| 124 | 1; | ||
| 125 | |||
| 126 | Now we need a result class which describes the user object. Create | ||
| 127 | C<lib/MyApp/Schema/Result/User.pm>: | ||
| 128 | |||
| 129 | package MyApp::Schema::Result::User; | ||
| 130 | use Moose; | ||
| 131 | extends 'DBIx::Class::Core'; | ||
| 132 | __PACKAGE__->table('user'); | ||
| 133 | __PACKAGE__->add_columns( | ||
| 134 | id => { is_auto_increment => 1, data_type => 'integer' }, | ||
| 135 | qw(email first last) | ||
| 136 | ); | ||
| 137 | __PACKAGE__->set_primary_key('id'); | ||
| 138 | 1; | ||
| 139 | |||
| 140 | To glue the DBIC schema and Catalyst together we create a model called | ||
| 141 | C<MyApp::Model::DBIC>. Paste the following in C<lib/MyApp/Model/DBIC.pm>: | ||
| 142 | |||
| 143 | package MyApp::Model::DBIC; | ||
| 144 | use Moose; | ||
| 145 | extends 'Catalyst::Model::DBIC::Schema'; | ||
| 146 | |||
| 147 | # we connect to an in-memory database | ||
| 148 | # which means that the database is reset | ||
| 149 | # with every start of the application | ||
| 150 | __PACKAGE__->config({ | ||
| 151 | schema_class => 'MyApp::Schema', | ||
| 152 | connect_info => ['dbi:SQLite:dbname=:memory:'] | ||
| 153 | }); | ||
| 154 | |||
| 155 | # this initializes the empty sqlite database | ||
| 156 | # and inserts one record | ||
| 157 | after BUILD => sub { | ||
| 158 | my $self = shift; | ||
| 159 | my $schema = $self->schema; | ||
| 160 | $schema->deploy; | ||
| 161 | $schema->resultset('User')->create({ | ||
| 162 | email => 'onken@netcubed.de', | ||
| 163 | first => 'Moritz', | ||
| 164 | last => 'Onken' | ||
| 165 | }); | ||
| 166 | }; | ||
| 167 | |||
| 168 | 1; | ||
| 169 | |||
| 170 | =head1 AUTHOR | ||
| 171 | |||
| 172 | Moritz Onken <onken@netcubed.de> | ||
| 173 | |||
| 174 | =head1 COPYRIGHT AND LICENSE | ||
| 175 | |||
| 176 | This software is Copyright (c) 2011 by Moritz Onken. | ||
| 177 | |||
| 178 | This is free software, licensed under: | ||
| 179 | |||
| 180 | The (three-clause) BSD License | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | |||
| 185 | __END__ | ||