[Jifty-commit] r3239 - in jifty/trunk: . lib/Jifty/View/Declare
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Sat May 12 19:05:51 EDT 2007
Author: jesse
Date: Sat May 12 19:05:51 2007
New Revision: 3239
Modified:
jifty/trunk/ (props changed)
jifty/trunk/lib/Jifty/Plugin/SiteNews/View/News.pm
jifty/trunk/lib/Jifty/View/Declare/CRUD.pm
Log:
r56812 at pinglin: jesse | 2007-05-12 19:05:30 -0400
* CRUD view and a working example (sitenews)
Modified: jifty/trunk/lib/Jifty/Plugin/SiteNews/View/News.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/SiteNews/View/News.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/SiteNews/View/News.pm Sat May 12 19:05:51 2007
@@ -5,10 +5,14 @@
use Jifty::View::Declare -base;
use Jifty::View::Declare::CRUD;
-template 'index.html' => page {
+import_templates Jifty::View::Declare::CRUD under '/';
+
+sub object_type { 'News' }
+sub fragment_base_path {'/news'}
- h1 { 'This is your site news'};
+template 'index.html' => page {
+ title is 'Site news' ;
form {
show('/news/list');
}
@@ -17,7 +21,7 @@
template 'view' => sub {
- my $self = 'Jifty::View::Declare::CRUD';
+ my $self = shift;
my ( $object_type, $id ) = ( $self->object_type, get('id') );
my $update = new_action(
class => 'Update' . $object_type,
@@ -41,14 +45,4 @@
};
-
-
-
-
-alias Jifty::View::Declare::CRUD under '/', { object_type => 'News', base_path => '/news',
- fragment_for_view => '/news/view',
- fragment_for_new_item => '/news/new_item'
-
-};
-
1;
Modified: jifty/trunk/lib/Jifty/View/Declare/CRUD.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View/Declare/CRUD.pm (original)
+++ jifty/trunk/lib/Jifty/View/Declare/CRUD.pm Sat May 12 19:05:51 2007
@@ -3,6 +3,9 @@
package Jifty::View::Declare::CRUD;
use Jifty::View::Declare -base;
+use base 'Exporter';
+our @EXPORT = qw(object_type fragment_for get_record);
+
sub object_type {
my $self = shift;
@@ -12,7 +15,17 @@
sub fragment_for {
my $self = shift;
my $fragment = shift;
- return $self->package_variable('fragment_for_'.$fragment)||($self->package_variable('base_path')|| '/crud')."/". $fragment;
+
+ if (my $coderef = $self->can('fragment_for_'.$fragment) ) {
+ return $coderef->($self);
+ }
+
+ return $self->package_variable('fragment_for_'.$fragment)||$self->fragment_base_path ."/". $fragment;
+}
+
+sub fragment_base_path {
+ my $self = shift;
+ return $self->package_variable('base_path')|| '/crud';
}
sub get_record {
@@ -26,63 +39,63 @@
}
template 'search' => sub {
- my $self = shift;
- my ( $object_type) = ($self->object_type);
-my $search = Jifty->web->new_action(
- class => "Search".$object_type,
- moniker => "search",
- sticky_on_success => 1,
-);
-
-div { { class is "jifty_admin" } ;
- render_action($search);
-
- $search->button(
- label => _('Search'),
- onclick => {
- submit => $search,
- refresh => Jifty->web->current_region->parent,
- args => { page => 1}
- }
- );
+ my $self = shift;
+ my ($object_type) = ( $self->object_type );
+ my $search = Jifty->web->new_action(
+ class => "Search" . $object_type,
+ moniker => "search",
+ sticky_on_success => 1,
+ );
- }
+ div {
+ { class is "jifty_admin" };
+ render_action($search);
+
+ $search->button(
+ label => _('Search'),
+ onclick => {
+ submit => $search,
+ refresh => Jifty->web->current_region->parent,
+ args => { page => 1 }
+ }
+ );
+
+ }
};
template 'view' => sub {
my $self = shift;
my ( $object_type, $id ) = ( $self->object_type, get('id') );
my $update = new_action(
- class => 'Update' . $object_type,
+ class => 'Update' . $object_type,
moniker => "update-" . Jifty->web->serial,
- record => $self->get_record( $id )
+ record => $self->get_record($id)
);
- div {{ class is 'crud read item inline' };
+ div {
+ { class is 'crud read item inline' };
hyperlink(
- label => "Edit",
- class => "editlink",
- onclick => {
- replace_with => $self->fragment_for('update'),
- args => { object_type => $object_type, id => $id }
- },
+ label => "Edit",
+ class => "editlink",
+ onclick => {
+ replace_with => $self->fragment_for('update'),
+ args => { object_type => $object_type, id => $id }
+ },
);
my @fields = grep {
!( m/_confirm/
- || lc $update->arguments->{$_}{render_as} eq
- 'password' )
- } $update->argument_names;
- render_action( $update, \@fields,
- { render_mode => 'read' } );
- hr {};
+ || lc $update->arguments->{$_}{render_as} eq 'password' )
+ } $update->argument_names;
+ render_action( $update, \@fields, { render_mode => 'read' } );
+ hr {};
};
};
template 'update' => sub {
my $self = shift;
- my ( $object_type, $id ) = ($self->object_type, get('id'));
+ my ( $object_type, $id ) = ( $self->object_type, get('id') );
my $record_class = Jifty->app_class( "Model", $object_type );
my $record = $record_class->new();
@@ -93,40 +106,43 @@
record => $record
);
- div {{ class is "crud update item inline " . $object_type }
+ div {
+ { class is "crud update item inline " . $object_type }
- div {{ class is 'crud editlink' };
+ div {
+ { class is 'crud editlink' };
hyperlink(
- label => "Save",
- onclick => [
- { submit => $update },
- { replace_with => $self->fragment_for('view'),
- args => { object_type => $object_type, id => $id }
- }
- ]
+ label => "Save",
+ onclick => [
+ { submit => $update },
+ { replace_with => $self->fragment_for('view'),
+ args => { object_type => $object_type, id => $id }
+ }
+ ]
);
hyperlink(
- label => "Cancel",
- onclick => {
- replace_with => $self->fragment_for('view'),
- args => { object_type => $object_type, id => $id }
- },
- as_button => 1
+ label => "Cancel",
+ onclick => {
+ replace_with => $self->fragment_for('view'),
+ args => { object_type => $object_type, id => $id }
+ },
+ as_button => 1
);
};
- render_action( $update );
+ render_action($update);
hr {};
- }
+ }
};
template 'list' => sub {
my $self = shift;
- my ( $object_type) = ($self->object_type);
-
- my( $page, $fragment_for_new_item, $item_path, $search_collection )= get(qw(page fragment_for_new_item item_path search_collection));
+ my ($object_type) = ( $self->object_type );
+
+ my ( $page, $fragment_for_new_item, $item_path, $search_collection )
+ = get(qw(page fragment_for_new_item item_path search_collection));
- $fragment_for_new_item ||= $self->package_variable('fragment_for_new_item');
+ $fragment_for_new_item ||= $self->fragment_for('new_item');
$item_path ||= $self->fragment_for("view");
my $collection_class
@@ -150,20 +166,21 @@
);
hyperlink(
- onclick => [{
- region => $search_region->qualified_name,
- replace_with => $self->fragment_for('search'),
- toggle => 1,
- args => { object_type => $object_type }
- },
- ],
- label => 'Toggle search'
+ onclick => [
+ { region => $search_region->qualified_name,
+ replace_with => $self->fragment_for('search'),
+ toggle => 1,
+ args => { object_type => $object_type }
+ },
+ ],
+ label => 'Toggle search'
);
outs( $search_region->render );
if ( $collection->pager->last_page > 1 ) {
- span {{ class is 'page-count' };
+ span {
+ { class is 'page-count' };
outs(
_( "Page %1 of %2", $page, $collection->pager->last_page ) );
}
@@ -173,36 +190,37 @@
outs( _("No items found") );
}
- div {{ class is 'list' };
+ div {
+ { class is 'list' };
while ( my $item = $collection->next ) {
render_region(
- name => 'item-' . $item->id,
- path => $item_path,
- defaults =>
- { id => $item->id, object_type => $object_type }
+ name => 'item-' . $item->id,
+ path => $item_path,
+ defaults => { id => $item->id, object_type => $object_type }
);
}
};
- div {{ class is 'paging' };
+ div {
+ { class is 'paging' };
if ( $collection->pager->previous_page ) {
- span {{ class is 'prev-page' };
+ span {
+ { class is 'prev-page' };
hyperlink(
- label => "Previous Page",
- onclick => {
- args =>
- { page => $collection->pager->previous_page }
- }
+ label => "Previous Page",
+ onclick => {
+ args => { page => $collection->pager->previous_page }
+ }
);
}
}
if ( $collection->pager->next_page ) {
- span {{ class is 'next-page' };
+ span {
+ { class is 'next-page' };
hyperlink(
- label => "Next Page",
- onclick => {
- args => { page => $collection->pager->next_page }
- }
+ label => "Next Page",
+ onclick =>
+ { args => { page => $collection->pager->next_page } }
);
}
}
@@ -210,9 +228,9 @@
if ($fragment_for_new_item) {
render_region(
- name => 'new_item',
- path => $fragment_for_new_item,
- defaults => { object_type => $object_type },
+ name => 'new_item',
+ path => $fragment_for_new_item,
+ defaults => { object_type => $object_type },
);
}
};
@@ -220,13 +238,14 @@
template 'new_item' => sub {
my $self = shift;
- my ( $object_type, $id ) = ($self->object_type, get('id'));
+ my ( $object_type, $id ) = ( $self->object_type, get('id') );
- my $record_class = Jifty->app_class("Model", $object_type);
- my $create = Jifty->web->new_action(class => 'Create'.$object_type);
+ my $record_class = Jifty->app_class( "Model", $object_type );
+ my $create = Jifty->web->new_action( class => 'Create' . $object_type );
- div {{ class is 'crud create item inline' };
- render_action( $create );
+ div {
+ { class is 'crud create item inline' };
+ render_action($create);
outs(
Jifty->web->form->submit(
@@ -234,9 +253,10 @@
onclick => [
{ submit => $create },
{ refresh_self => 1 },
- { element => undef,#$region->parent->get_element('div.list'),
- append => $self->fragment_for('view'),
- args => {
+ { element =>
+ undef, #$region->parent->get_element('div.list'),
+ append => $self->fragment_for('view'),
+ args => {
object_type => $object_type,
id => { result_of => $create, name => 'id' },
},
@@ -244,52 +264,7 @@
]
)
);
- }
-};
-
-# render tabview using yui.
-
-# if a tab ends in _tab, it means it should contain a stub region to
-# be replaced by the corresponding fragment onclick to that tab.
-
-sub render_tabs {
- my ($self, $divname, $args, @tabs) = @_;
-
- outs_raw(qq'<script type="text/javascript">
- var myTabs = new YAHOO.widget.TabView("$divname");
- </script>' );
-
-
- div { { id is $divname, class is 'yui-navset'}
- ul { { class is 'yui-nav'};
- my $i = 0;
- for (@tabs) {
- my $tab = $_;
- li { { class is 'selected' unless $i };
- hyperlink(url => '#tab'.++$i, label => $tab,
- $tab =~ s/_tab$// ?
- (onclick =>
- { region => Jifty->web->current_region->qualified_name."-$tab-tab",
- replace_with => $self->fragment_for($tab),
- args => { map { $_ => get($_)} @$args },
- }) : ()
- ) }
- }
- };
- div { {class is 'yui-content' };
- for (@tabs) {
- div {
- if (s/_tab$//) {
- render_region(name => $_.'-tab');
- }
- else {
- die "$self $_" unless $self->has_template($_);
- $self->has_template($_)->();
- }
- }
- }
- }
- };
+ }
};
1;
More information about the Jifty-commit
mailing list