In an earlier version of my code, I did some dynamic JavaScript 
configuration using Mako constructs:

    # app.mako
    <script>
        % if something:
            // set some JS var
        % endif
        var x = '${some_var_from_view}';
        var y = ${something_that_is_specific_to_this_request(request)};
    </script>

Now I do something like this:

    # helpers.py
    def get_js_config(request):
        config = {
            # whatever request-specific config you need
        }
        return literal(json.dumps(config))

    # app.mako
    <script src="main.js"></src>  ## Load main JS function
    <script>
        main(${h.get_js_config()});
    </script>

I don't know if that is be applicable to your situation, but maybe you 
could save the config object as a global that you can reference in 
subsequent AJAX calls.

If I were going to go with the template approach, I'd set one config var 
and reference that in the rest of the code rather than doing a bunch of 
interpolation:

    # snippet.js
    (function () {
        var config = ${js_config};  // js_config is a JSON string
        // ...
        if (config.xyz) {
            // do stuff
        }
    }());


On Sunday, December 18, 2011 9:32:38 PM UTC-8, Ahmed wrote:
>
> Interesting Wyatt. What do you mean by a JSON/JS approach? So you
> factor it in a way that you won't have to use templates for doing
> string replacements? Can you give a small example?
>
> I already have the static files jquery etc. as separate static
> resources.
> However, I have these functions that however need to be altered a bit
> in the js code itself.
>
> Also, another disadvantage is that inline code in script tags does not
> get run when it  is loaded via a jquery AJAX. The opposite (as in a
> separate js file) however is true. And I wonder if it is better to be
> able to serve these small snippets as separate js files rather than
> inline, so that they get loaded even if downloaded via AJAX. I guess
> then I will have to assign a special view/route to these dynamic
> snippets, and render via a template.
>
> Ahmed
>
> On Dec 19, 9:32 am, Wyatt Baldwin <[email protected]> wrote:
> > On Sunday, December 18, 2011 9:39:28 AM UTC-8, Chris Rossi wrote:
> >
> > > On Sun, Dec 18, 2011 hat 7:55 AM, Ahmed <[email protected]> wrote:
> > > > I wonder what is the best practice for my case? And if using a
> > > > template renderer is the best solution, how is that best made? For
> > > > example is the Chameleon text template suitable for this job?
> >
> > > That's what I would use.  I'm usually able to factor my js in such a
> > > way, though, that my js files can be served statically and only depend
> > > a few lines of dynamic data injected into the html page in a <script>
> > > tag.
> >
> > Similarly, I create a single JSON config object in a helper function and
> > inject it into the template; it gets passed to a main JavaScript function
> > (defined in a separate script file), which sort-of reflects how a Pyramid
> > app is initialized.
> >
> > For a while, I did some dynamic JS setup using Mako, but it was a bit
> > messy, and I prefer the straight JSON/JS approach.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/pylons-discuss/-/M-PHaIWZMuQJ.
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