简体   繁体   中英

Symfony2 Controller,View and JavaScript

I am doing a project under Symfony2. I got two actions in the same controller (historic, calender), and two views.

My historic action calls calendar action, with default value of this week. Then it renders historic view, which himself renders calendar view.

First question, its working, but before calender HTML view, symfony have add a sentence : HTTP/1.0 200 OK Cache-Control: no-cache Date: Wed, 30 Jan 2013 10:17:13 GMT how can I drop this sentence ?

I am doing this, because I got some JavaScript links who make the calendar go for last or next week by calling calendar Controller (with different previous or next week), and print new calendar in his div. It's kind of working, but I got some strange behaviour. I have put JavaScript code in calendar view. First time I load the page JavaScript shows in code source, and work. If I press previous or next week button, calendar controller returns me new view of calendar but without the javascript. I can not see JS no more in firebug. But (and that's the most strange part), JavaScript still works (if I press button it reload calendar div).

Here is some of my code :

calendar.html.twig :

<script type="text/javascript">
$('.nextWeek').live('click', function () {
    $.ajax({
        url: 'prof_calendrier/{{ nextWeek }}',
        success: function (html) {$('.calendrier').html(html);}
    });
});
$('.lastWeek').live('click',function () {
    $.ajax({
        url: 'prof_calendrier/{{ lastWeek }}',
        success: function (html) {$('.calendrier').html(html);}
    });
});
</script>
<p>Week from {{datesWeek.0}} to {{datesWeek.6}}</p>
[...] display rest of informations

prof_calendrierAction (calendar action) :

public function prof_calendrierAction ($date) {
        $erreur='';
        $message='';
        $session = $this->container->get('session');

        [...] // lot of operations

    return $this->render('CDUserBundle:Default:prof_calendrier.html.twig', array(
        'next_cours' => $liste_prochains_cours,
        'today' => date('D'),
        'nextWeek' => $nextWeek,
        'lastWeek' => $lastWeek,
        'datesWeek' => $datesWeek
    ));
}

Can someone help me please ?*

Edit : To avoid, HTTP message, my historic action does not call calendar action any more. I render historic, and in historic view, I got ajax init who will check calendar action with todays week. But when I refresh JavaScript with last or next week button, html changes, but JavaScript is not updated. JavaScript is wrote in calendar view, so why calendar action do not return JS more than the first time.

Edit²: I did several tests. My JavaScript for prev/next week button is in calendar view. It's like if all the JS lines which are written in this view are being catch and cached for some times. I mean the first time I press next week, it show next weeks no problem. The second time, firebugs show me two ajax request, one for the same week and one for the one after. It's like if the first JavaScript lines were being kept and cached, then on the second press of next week, it calls the old one, and then the new one...

You could use the response component of Symfony to send specific response from your controller. Add

use Symfony\Component\HttpFoundation\Response;

then build your "html" variable response and send it like that:

return new Response($return,200,array('Content-Type'=>'application/json'));

Here, $return is a json entity. 200 is the status code of the response, and 'Content-Type'=>'application/json' has to match the type of $return.

This is how I did with my Symfony2 calendar and it works fine.

EDIT FOR JS: you can't change your javascript via ajax using twig. You should define global variables in your javascript and follow through them whatever parameter you need. These global variables are accessible in your javascript when you treat the ajax response. For instance, you could do in twig:

<script>
var globalMonth = {{ month }}; // set up the starting month
...

And then when ajax send the response, you update globalMonth and use it for the next ajax call.

Alban

I think I know what is going on with your JS. When you have an ajax response, you probably bind a new action to the click event on the nextWeek link. But before adding that action, you have to do

$('#nextWeek').unbind();

in order to get rid of the previous binded events.

Instead of $this->render() , call $this->renderView() in your second controller.

For a better use of Symfony use the path or url functions in your controller to generate the url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM