Template templates for javascript files?

I am developing a simple pyramid application where I use jQuery to execute AJAX requests. I still had my javascript code in my chameleon patterns. Now I want to extract my javascript to another location (e.g. as static resources).

My problem is that I believe that my javascript code relies on dynamically generated content:

$.post("${request.route_url('my_view')}",{'data': 'some data'}, function(html){
    $("#destination").html(html);
});

Dynamic Element:

"${request.route_url('my_view')}"

Which calls the route_url method of the request object in the template.

Is there a recognized template for splitting such javascript files into my own templates and providing routes and views for them, or am I just saving my javascript in my page template?

+5
source share
1 answer

; - , ​​ , () JavaScript.

, :

  • HTML:

    <body data-viewurl="http://www.example.com/route/to/view">
        ...
    </body>
    

    JS- jQuery .data():

    var viewurl = $('body').data('viewurl');
    
  • LINK :

    <head>
        <link rel="ajax-datasource" id="viewurl"
              href="http://www.example.com/route/to/view" />
        ...
    </head>
    

    $('link#viewurl').attr('href') $('link[rel=ajax-datasource]').attr('href'). URL.

  • JS- , :

    <head>
        ...
        <script type="text/javascript">
           window.contextVariables = {
               viewurl = "http://www.example.com/route/to/view",
               ...
           };
        </script>
    </head>
    

    contextVariables.viewurl.

+9

All Articles