duanhuang1967 2010-02-01 14:52
浏览 23

在symfony的部分视图中的Javascript源代码

I'm using symfony 1.4, I wrote a piece of js code to use in a template, and I want to put it in a JS separated file because I'll use it many times in the code.

I added the JS to the template using:

<?php use_javascript('mi_js') ?>

This templates has some ajax calls that refresh zones of the view with renderPartial method. This new zones also use the JS code, so I need to add this code in the partial view. But if add:

<?php use_javascript('mi_js') ?>

in the partial, then it doesn't work. To get this work I have to put all the JS code in the partial, like:

<script type="text/javascript">/*<![CDATA[*/

$('.mi_class').click(function() {

 var a = $(this).parent();
...

As I told I don't want to do this.

Any idea what can I do? Any template method to do this?

Thanks in advance. Alejandro G.

  • 写回答

3条回答 默认 最新

  • dongshuan8722 2010-02-01 15:35
    关注

    The reason why you have to put the code in the partial is the following:

    When you use use_javascript('mi_js') then the (path to the) JS file gets added to the sfResponse object. Then, when the templates get rendered, all the JS files get included into the layout file via get_javascripts().

    But now as you only render the partial and send the results back via Ajax, the JS files get not included.

    I suggest to put your code into a function and add it to the header of your HTML file. Then in the partials you call:

    <script type="text/javascript">/*<![CDATA[*/
    
    $('.mi_class').click(the_new_function())
    

    (Maybe you have to define parameters, I don't know).

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?