On Tue, Aug 17, 2010 at 8:21 PM, Wyatt Baldwin
<[email protected]> wrote:
> On Aug 17, 4:37 pm, Richard Harding <[email protected]> wrote:
>> On Tue, Aug 17, 2010 at 5:52 PM, Wyatt Baldwin
>>
>> <[email protected]> wrote:
>> > I'm wondering if anyone has any good ideas on how to generate URLs in
>> > JavaScript code that take into account the application's URL prefix.
>> > What I've done is implement a couple of JS helpers to sort-of mimic
>> > `pylons.url`, and this seems to work pretty well.
>>
>> What I do is add a variable to the tpl context:
>>
>> tmpl_context.app_url=request.application_url
>>
>> then in my wrapper.mako file which wraps all of my html pages I define
>> this as a JS variable before all other JS in the <head> tags.
>>
>> <script type="text/javascript" charset="utf-8">
>> app_namespace.app_url = '${c.app_url}';
>> </script>
>>
>> And now in all my JS files included/etc I can use
>> app_namespace.app_url for all actions.
>
> So, does your JS code look like this?:
>
> my_favorite_js_library.async_request({
> url: app_namespace.app_url + '/controller/action/id'
> });
>
> That's what my first approach looked like (and essentially that's all
> my little function is doing).
>
Basically, I have a custom wrapper around jQuery's ajax request
functionality that defaults to common config values for my app (always
GET for instance) and so I've just hard coded that wrapper to always
check for app_namespace.app_url so that I don't have to enter it into
more than one location.
>> > In my Mako template, I do this:
>>
>> > <script>
>> > var MY_NAMESPACE_CONFIG = {/* config values */};
>> > MY_NAMESPACE_CONFIG.urls = {
>> > prefix: '${url("/").rstrip("/")}'
>> > };
>> > </script>
I try to keep all my javascript out into a external JS files vs in my
mako code. This way I get better editor support for things since it's
just JS, I can minimize it all safely, and I don't have any JS/Mako
combo issues.
>> > On a related note, how do you pass config values from Pylons to your
>> > JavaScript code? I convert a subset of my Paste config to JSON in
>> > `load_environment` and decode it in the template.
What config values do you need from your pylons app to JS? I've not
run into any other than the url I've needed to worry about. I guess if
I had additional ones I'd just treat them the same as the app_url.
Rick Harding
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.