[Catalyst] Store something in the stash at startup

Octavian Rasnita octavian.rasnita at ssifbroker.ro
Wed Aug 18 05:33:31 GMT 2010


From: "Charlie Garrison" <garrison at zeta.org.au>
Good afternoon,

On 17/08/10 at 4:24 PM +0100, Tomas Doran
<bobtfish at bobtfish.net> wrote:

>On 17 Aug 2010, at 15:50, Charlie Garrison wrote:
>>
>>I was suggesting the Moose attribute as way of storing the model in $c to 
>>be easier to reference from templates.
>
>I highly recommend taking the model instance and stashing it (as is being 
>done already), rather than making an attribute on the context object, as 
>this is less flexible...
>
>I.e. if you are saying [% c.model('Foo').thing %] or [% c.foo.thing %] then 
>this is less flexible (as you can't override foo for part of your site) 
>than $c->stash(foo => $c->model('Foo')) in your perl code, then [% 
>foo.thing %] in your template.

I'm not sure I understand why it's less flexible. I use
attributes on the context object to store objects that are
needed *everywhere* in the app. I was following the logic of
$c->user which stores the current $user. The objects stored in
the context attributes are still models, and as such can be flexible.

It didn't make sense to me (& in one case had race-condition,
don't recall specifics) to store all those objects in the stash
for each request. So I'm using this as an opportunity to learn...

If the stash or the context attribute are simply storing a model
object, what is the advantage to storing in stash over context
attribute? If it's the model that I would want to
override/extend, why does it matter whether it's accessed via
stash or via context attribute?

And thanks for the input so far, always fun to learn more.  :-)


Charlie



Hi Charlie,

It is more flexible to use the stash, because you can use the same model 
under different names. At least this is a flexibility advantage. There may 
be others that I don't know.

In a controller you may use:

$c->stash(
menu => $c->model('Menu'),
second_menu => $c->model('Menu'),
);

For the second menu you may eventually provide some aditional parameters to 
a method offered by the object...

This flexibility would not be possible when using a method of the context 
object directly.

Octavian


__________ Information from ESET NOD32 Antivirus, version of virus signature database 5374 (20100817) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com






More information about the Catalyst mailing list