Преглед на файлове

Evolution #90: Ajaxsification des actions

bastien преди 13 години
родител
ревизия
4c9fd1c4e1
променени са 3 файла, в които са добавени 54 реда и са изтрити 2 реда
  1. 24 0
      src/Muzich/CoreBundle/lib/Controller.php
  2. 18 2
      src/Muzich/FavoriteBundle/Controller/FavoriteController.php
  3. 12 0
      web/bundles/muzichcore/js/muzich.js

+ 24 - 0
src/Muzich/CoreBundle/lib/Controller.php Целия файл

7
 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
7
 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
8
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
8
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
9
 use Muzich\CoreBundle\Form\Element\ElementAddForm;
9
 use Muzich\CoreBundle\Form\Element\ElementAddForm;
10
+use Symfony\Component\HttpFoundation\Response;
10
 
11
 
11
 class Controller extends BaseController
12
 class Controller extends BaseController
12
 {
13
 {
258
     );
259
     );
259
   }
260
   }
260
   
261
   
262
+  /**
263
+   * Retourne une réponse contenant du json
264
+   * 
265
+   * @param array $content
266
+   * @return Response 
267
+   */
268
+  protected function jsonResponse($content)
269
+  {
270
+    $response = new Response(json_encode($content));
271
+    $response->headers->set('Content-Type', 'application/json; charset=utf-8');
272
+    return $response;
273
+  }
274
+  
275
+  protected function getAssetUrl($path, $packageName = null)
276
+  {
277
+    return $this->container->get('templating.helper.assets')->getUrl($path, $packageName);
278
+  }
279
+  
280
+  protected function trans($string, $params = array(), $package = null)
281
+  {
282
+    return $this->get('translator')->trans($string, $params, $package);
283
+  }
284
+  
261
 }
285
 }

+ 18 - 2
src/Muzich/FavoriteBundle/Controller/FavoriteController.php Целия файл

62
     
62
     
63
     if ($this->getRequest()->isXmlHttpRequest())
63
     if ($this->getRequest()->isXmlHttpRequest())
64
     {
64
     {
65
-      
65
+      return $this->jsonResponse(array(
66
+        'favorite'      => true,
67
+        'link_new_url'  => $this->generateUrl('favorite_remove', array(
68
+            'id'    => $id,
69
+            'token' => $token
70
+        )),
71
+        'img_new_src'   => $this->getAssetUrl('bundles/muzichcore/img/favorite.png'),
72
+        'img_new_title' => $this->trans('element.favorite.remove', array(), 'elements')
73
+      ));
66
     }
74
     }
67
     else
75
     else
68
     {
76
     {
101
     
109
     
102
     if ($this->getRequest()->isXmlHttpRequest())
110
     if ($this->getRequest()->isXmlHttpRequest())
103
     {
111
     {
104
-      
112
+      return $this->jsonResponse(array(
113
+        'favorite'      => true,
114
+        'link_new_url'  => $this->generateUrl('favorite_add', array(
115
+            'id'    => $id,
116
+            'token' => $token
117
+        )),
118
+        'img_new_src'   => $this->getAssetUrl('bundles/muzichcore/img/favorite_bw.png'),
119
+        'img_new_title' => $this->trans('element.favorite.add', array(), 'elements')
120
+      ));
105
     }
121
     }
106
     else
122
     else
107
     {
123
     {

+ 12 - 0
web/bundles/muzichcore/js/muzich.js Целия файл

140
      $(location).attr('href', $('input.filter_mytags_url').val());
140
      $(location).attr('href', $('input.filter_mytags_url').val());
141
    });
141
    });
142
    
142
    
143
+   // Mise en favoris
144
+   $('a.favorite_link').click(function(){
145
+     link = $(this);
146
+     $.getJSON($(this).attr('href'), function(response) {
147
+       img = link.find('img');
148
+       link.attr('href', response.link_new_url);
149
+       img.attr('src', response.img_new_src);
150
+       img.attr('title', response.img_new_title);
151
+     });
152
+     return false;
153
+   });
154
+   
143
  });
155
  });
144
  
156
  
145
  
157