Perrin Harkins wrote:
> Hi,
>
> I have some data which Id like to share between components.  I'm
> fetching things from the database in a MasonX::WebApp class and then
> displaying it in my Mason templates.  However, I'm including other
> components, and the included components need access to the data too.
> This results in me having to do pass it everywhere: <& foo.html, %ARGS
> %>.
>
> This seems like a poor solution.  Is there an obvious way to deal with
> this?  Maybe stuffing the data in $m->notes instead of %ARGS?  It
> seems a little odd to put all my data there, but it might be less
> awkward.
>   
I'm not sure if this is a bug or a feature but this works for me under 
HTML::Mason 1.26:

1. Create stash.comp that has mutator ..getset which accesses a %Stash 
variable created in the <%shared> block.
2. parent.comp calls stash.comp to get an object instance and calls 
.set_data to store things
3. parent.comp then calls child.comp.
4. child.comp calls stash.comp to get an object instance and calls .get_data

Here is a minimal example:

__stash.comp__

    <%shared>
    my %Stash;
    my $self = $m->base_comp;
    </%shared>

    <%init>
    return $self;
    </%init>

    <%method .getset>
    <%init>
    my ($field, $value) = @_;
    die "Cannot continue without field parameter"
      unless defined $field;

    $Stash{$field} = $value if @_ > 1;

    return $Stash{$field};
    </%init>
    </%method>

__caller.comp__

    <%init>
     my $stash = $m->comp("stash.comp");

    $stash->call_method('.getset', title => 'Test Page' );

    </%init>

    The title is:
      [<% $stash->call_method('.getset', 'title') %>]<br>

    test-stash-inner thinks the title is:
      [<& 'test-stash-inner.comp' &>]<br>

    Calling the method without an instance returns:
      [<% $m->comp('stash.comp:.getset', 'title') %>]<br>

__test-stash-inner.comp__

    % my $stash = $m->comp("stash.comp");
    <% $stash->call_method('.getset', 'title') %>

__output__

    The title is: [Test Page]
    test-stash-inner thinks the title is: [Test Page ]
    Calling the method without an instance returns: [Test Page]

Apparently items stored in the shared block are shared even when called 
from other components.  It's almost as if the shared block is a 
singleton that persists for the duration of the request.

-- BenRifkah

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users

Reply via email to