Re: [Catalyst] Passing connection info to an custom model/external lib

2009-04-11 Thread Ian Docherty

Eric Wright wrote:

I have what I hope is not a dumb question. I'm trying to follow best
practices here and am writing a piece of business logic that I feel
should be Catalyst agnostic and therefore am creating a custom model.
(However, at the moment Catalyst is the primary consumer of this lib.)
I'd like to use my DBIC::Schema goodness for DB access and happily
have abstracted that via Catalyst::Model::DBIC::Schema. The thing is I
realized that if I connect to this schema outside of the scope of
Catalyst via DBIx::Class::Schema-connect I have to pass in the
connection info separately which really bugs me because I've already
defined it via a configuration file for the Catalyst app (using
Config::General). I suppose I could just pass that info into the model
but that seems messy and redundant to me. If I subclass
Catalyst::Model well then I'm tightly coupled with Catalyst again. I'd
like to just be able to glue the model to Catalyst and do a:

$c-model('MyCustomModel')-foo(...)

and it just work.

It seems to me there should be a simple way for this schema to know
its connection info based on context and I figured there's like a
slick Catalyst ninja kind of method for doing this that my brain is
just not grasping at the moment. How do you do this sort of thing? Is
the magic in the Catalyst glue?

Thanks in advance for any advice/thoughts/comments...

-Eric
  
I use an external yaml file to hold connection information. In that way 
I can easily have different

configurations for different environments, dev, staging, live.

# production database access
DBIxProd:
   dsn:'DBI:mysql:host=localhost;database=myapp_prod'
   username:   fred
   password:   secret
...

In my Catalyst model I do something like.

package myapp::Model::DBIxProd;

use strict;
use base 'Catalyst::Model::DBIC::Schema';

__PACKAGE__-config(
   schema_class= 'myapp::Storage',
   connect_info = [
   myapp-config-{DBIxProd}{dsn},
   myapp-config-{DBIxProd}{username},
   myapp-config-{DBIxProd}{password},
   { 'mysql_enable_utf8' = 1 },
   { on_connect_do =[ 'set names utf8' ] },
   ]);

1;

And in my external app (usually in a 'tools' subdirectory) I do 
something like.


use FindBin;
use lib $FindBin::Bin/../lib;

use myapp::Storage;
use YAML;

my $config = YAML::LoadFile($FindBin::Bin/../myapp.yml);

my $schema = myapp::Storage-connect(
   $config-{DBIxProd}{dsn},
   $config-{DBIxProd}{username},
   $config-{DBIxProd}{password},
   { mysql_enable_utf8  = 1 },
   { on_connect_do = [ 'set names utf8' ] },
);


Regards
Ian

___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Application stallling problem

2009-04-11 Thread kakimoto
Hello, Kieren,

Good morning. Thank you for your help. I have the templates below. 
My apologies if it is too long but I really need some pointers here.

The main file, home/kakimoto/projects/myApp/root/src/index.tt2 decides
if we show the general static content if the user is not logged in. If
the user is logged in, we display the menu.

My problem happens site wide be it the user is logged in or not.
 For example, clicking on either of the static page links, Test Drive,
About us and so forth (within 10 seconds) causes the myapp_server.pl
screen in my terminal to not do anything (ie it freezes up).


is it true that myapp_server.pl is not good enough to handle requests
that are established within 10 seconds or less? Or even double clicks?

Thank you for your help. Any comment would be appreaciated.





1) /home/kakimoto/projects/myApp/root/src/index.tt2
=

[% IF Catalyst.user_exists %]
[% PROCESS menu.tt2 %]
[% ELSE %]
!--
User has not logged in. 
--

!--
   this section displays the general site page  (ie the general contents)
--
[% END %]


2) /home/kakimoto/projects/myApp/root/src/sidebar.tt2


!-- Sidebar code [start] --
a href=/menu class=sidebar
Main 
/a
br/
a href=/users/profile class=sidebar
Profile
/a
br/
a href=/users/subscriptions class=sidebar
Subscriptions
/a
br/
a href=/listings/search class=sidebar
Search Listings
/a
br/
a href=/users/reporting class=sidebar
Reporting
/a
br/
a href=/users/export_logs class=sidebar
Export logs
/a
!-- Sidebar code [end] --



3) /home/kakimoto/projects/myApp/root/src/agents/create.tt2


[%  USE Dumper(Indent=1) -%]

[% META title = 'Sign up' %]
!-- 
Load agent form [start]
--

h1 [% page_title %] /h1

[% IF signup_success and message %]
div id=message
[% message %]
/div
[% ELSE %] 
[%
#  target_action =
Catalyst.uri_for('/sign_up');
PROCESS 'agents/form.tt2';
%] 
[% END %]
!--
Load agent form [end]
--

4) /home/kakimoto/projects/myApp/root/src/agents/form.tt2


[%  USE Dumper(Indent=1) -%]


[% META title = 'Sign up/agent profile' %]

[% element_requires_validation = 'font
id=requires_validation*/font' %]

[% IF message %]
div id=message
[% message %]
/div

[% END %]
pre
[% #Dumper.dump(errors) %] 
/prE

[% IF errors %]
br/
div id=error
ul
[% FOREACH error IN errors %]
li[% error %]/li
[% END %]
/ul
/div
hr/
[% END %]
form method=post action=[% target_action
%] id=agent_profile_form name=agent_profile_form
table border=0 cellspacing=3
[% IF sign_up %]
tr
td valign=top
h2 Login Credentials/h2
[% PROCESS
agents/login_credentials.tt2 %]
/td
/tr
[% END %]

tr
td valign=top
h2Company/h2
[% PROCESS
agents/company_details.tt2 %]
/td
/tr

tr
td valign=top
table
tr
td
h2Address/h2
   

Re: [Catalyst] Gen. principles for JS integration?

2009-04-11 Thread Peter Karman
Jesse Sheidlower wrote on 4/11/09 9:51 PM:

 
 I don't know the best way to handle this. I'd expect that what
 I want to do is pass a variable like url_to_call to the JS,
 but I don't know how to get it there. I can certainly have a
 script block in my TT file that simply sets the JS variable
 from the stash, but this feels like a kludge. I assume there's
 also some way to tweak the controller so that this relative
 URL will work properly with chained controllers, but this also
 feels like a kludge.

It might be a kludge, but it's what I do.

I use YUI and ExtJS (with some jQuery). The namespace creation in YUI/ExtJS is
nice, so I can make a YUI.myApp namespace, e.g.

 script type=text/javascript
  YUI.myApp.BaseURL = '[% c.uri_for('whatever') %]'; // set constant
 /script

I tend to render whatever can't be put in a .js file in script tags, often
building up a TT hashref and then rendering it as JSON:

 [%
SET myJsonConfig = {
   foo = 'bar',
   ...
};
  %]

  script
var myFooThing = YUI.myApp.initFooThing([% myJsonConfig.as_json %]);
  /script

See Template::Plugin::Handy for the as_json vmethod.

-- 
Peter Karman  .  http://peknet.com/  .  pe...@peknet.com

___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/