[Catalyst] Gen. principles for JS integration?

Peter Karman peter at peknet.com
Sun Apr 12 03:48:55 GMT 2009


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/  .  peter at peknet.com



More information about the Catalyst mailing list