python - Chameleon templates for javascript files? - Stack Overflow

I am developing a simple pyramid application where I am using JQuery to do AJAX requests. I have until

I am developing a simple pyramid application where I am using JQuery to do AJAX requests. I have until now had my javascript code within my chameleon templates. Now I want to extract my javascript into another location (e.g. as static resources).

My problem is that I find my javascript code relies on dynamically generated content like so:

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

The dynamic element being:

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

Which is calling the route_url method of the request object within the template.

Is there a recognised pattern for separating such javascript files into their own templates and providing routes and views for them or do I simply keep my javascript in my page template?

I am developing a simple pyramid application where I am using JQuery to do AJAX requests. I have until now had my javascript code within my chameleon templates. Now I want to extract my javascript into another location (e.g. as static resources).

My problem is that I find my javascript code relies on dynamically generated content like so:

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

The dynamic element being:

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

Which is calling the route_url method of the request object within the template.

Is there a recognised pattern for separating such javascript files into their own templates and providing routes and views for them or do I simply keep my javascript in my page template?

Share Improve this question edited Jul 27, 2012 at 16:36 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Jun 22, 2012 at 12:14 Graeme StuartGraeme Stuart 6,0632 gold badges28 silver badges46 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Yes; you generally put context-specific information like expanded routes into the templates and access this information from your (static) JavaScript libraries.

Including the context info can be done in various ways, depending on taste:

  1. You could use a data attribute on a tag in your generated HTML:

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

    which you then, in your static JS code load with the jQuery .data() function:

    var viewurl = $('body').data('viewurl');
    
  2. Use a made-up LINK tag relationship to include links in the document head:

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

    which can be retrieved using $('link#viewurl').attr('href'), or $('link[rel=ajax-datasource]').attr('href'). This only really works for URL information.

  3. Generate JS variables straight in your template, to be referenced from your static code:

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

    and these variables are referable directly with contextVariables.viewurl.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745419288a4626894.html

相关推荐

  • python - Chameleon templates for javascript files? - Stack Overflow

    I am developing a simple pyramid application where I am using JQuery to do AJAX requests. I have until

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信