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.

Rick Harding




>
> In my Mako template, I do this:
>
> <script>
>    var MY_NAMESPACE_CONFIG = {/* config values */};
>    MY_NAMESPACE_CONFIG.urls = {
>        prefix: '${url("/").rstrip("/")}'
>    };
> </script>
>
> And in my util code, there's this function:
>
>  url: function (path, params) {
>    // It's assumed that ``path`` starts with a forward slash
>    path = [MY_NAMESPACE_CONFIG.urls.prefix, path].join('');
>    if (params) {
>      var query_string =
> my_namespace.util.paramsToQueryString(params);
>      path = [path, query_string].join('?');
>    }
>    return path;
>  }
>
> Not that this is overly complicated, but does anyone know of a better
> or simpler approach?
>
> 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.
>
> --
> 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.
>
>

-- 
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.

Reply via email to