Reputation: 672
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.
Upvotes: 0
Views: 154
Reputation: 9188
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()
.
Upvotes: 1
Reputation: 5203
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}
}
Upvotes: 0