0

I have an action in one controller that needs some db records stashed to render a table.

I have an action in a second controller that fills a very similar table, and so the data in its own stash is the same as I need for the first one.

Without duplicating a bunch of code in both controllers, is there a way to use one action to fill the stash of the other?

EDIT: In response to code request (somewhat simplified, but should get the gist across):

In Contoller::ShoppingCart

sub data : Chained('base') PathPart('') CaptureArgs(0) {
my ( $self, $c ) = @_;

my $query = DBRESULTS;
    my $count = 20;

$c->stash(
    data         => $items,
    dataRowCount => scalar @$items,
    totalCount   => $count,
    pageSize     => $pageSize,
);
}

In Controller::Vendor

sub viewform : Chained('row') Args(1) {
    my ( $self, $c, $view ) = @_;

    $c->stash(
        template => 'simpleItem.mas',
        view => $view,
    );
}

The simpleItem.mas template requires data, dataRowCount, totalCount, pageSize, so grabbing the stash from Controller::ShoppingCart::pageData would be ideal.

Ryan
  • 672
  • 1
  • 6
  • 16

2 Answers2

1

You should be able to simply $c->forward() to the specific action you need.

sub viewform : Chained('row') Args(1) {
    my ( $self, $c, $view ) = @_;

    $c->forward('Controller::ShoppingCart', 'data', [ @optional_args ]);

    $c->stash(
        template => 'simpleItem.mas',
        view => $view,
    );
}

All the gory details, including the siblings of forward(), like detach(), go() and visit().

RET
  • 9,100
  • 1
  • 28
  • 33
0

You could chain both actions off of another function, say in Root.pm, that would get these db records and put them in the stash for you.

i.e.:

# in Root.pm
sub get_db_stuff :Path('') :Args(0) {
    my ( $self, $c ) = @_;

    #store stuff in stash
    $c->stash->{db} = #get db records
}

Then your in your other controllers where your actions are you would need these two functions:

sub base : Chained('/get_db_stuff') PathPrefix CaptureArgs(0) {}

sub use_db_stuff : Chained('base') PathPart Args(0) {
    my ( $self, $c ) = @_; 

    #db records now accessible through $c->stash->{db}
}
srchulo
  • 5,143
  • 4
  • 43
  • 72
  • Good idea, but unfortunately, they are both already pretty dependent on chains themselves, so I can't really change what they are chained to. – Ryan Jan 30 '13 at 06:02