example of how to create forms that return users to the URI they came from
groditi [Sat, 1 Aug 2009 20:57:29 +0000 (20:57 +0000)]
lib/ComponentUI/Controller/TestModel/Bar.pm
share/skin/componentui/layout/bar/create.tt [new file with mode: 0644]

index 4859b7d..95f0975 100644 (file)
@@ -8,6 +8,7 @@ __PACKAGE__->config(
   collection_name => 'Bar',
   action => {
     base => { Chained => '/base', PathPart => 'testmodel/bar' },
+    create => { ViewPort => { layout => 'bar/create' } },
     list => {
       ViewPort => {
         enable_order_by => [qw/name foo published_at/],
@@ -29,7 +30,27 @@ sub create :Chained('base') {
   my $action_vp = $self->next::method(@_);
   my $self_uri = $c->uri_for($self->action_for('create'));
   $action_vp->action($self_uri);
+
+  my $params = $c->request->parameters;
+  if ( defined $params->{return_to_uri} && $params->{return_to_uri} ){
+    if( $params->{return_to_uri} ne $c->request->uri ){
+      $action_vp->layout_args->{return_to_uri} = $params->{return_to_uri};
+    }
+  } elsif( $c->request->referer ne $c->request->uri) {
+    $action_vp->layout_args->{return_to_uri} = $c->request->referer;
+  }
+
   return $action_vp;
 }
 
+sub on_create_close_callback {
+  my($self, $c, $vp) = @_;
+  if ( my $return_to_uri =  delete $c->request->parameters->{return_to_uri} ){
+    $c->response->redirect( $return_to_uri );
+  } else {
+    $self->redirect_to( $c, 'list' );
+  }
+  $c->detach;
+}
+
 1;
diff --git a/share/skin/componentui/layout/bar/create.tt b/share/skin/componentui/layout/bar/create.tt
new file mode 100644 (file)
index 0000000..e59fbba
--- /dev/null
@@ -0,0 +1,10 @@
+=widget Action
+
+=extends action
+
+=for layout container_list
+
+<input type="hidden" name="return_to_uri" value="[% return_to_uri %]">
+[% call_next %]
+
+=cut