Browse Source

Evolution #67: Tags: requetes ajax: eviter les retour

bastien 13 years ago
parent
commit
a268756c2b

+ 7 - 2
src/Muzich/CoreBundle/Controller/SearchController.php View File

@@ -59,7 +59,7 @@ class SearchController extends Controller
59 59
    *
60 60
    * @param string $string_search 
61 61
    */
62
-  public function searchTagAction($string_search)
62
+  public function searchTagAction($string_search, $timestamp)
63 63
   {
64 64
     if ($this->getRequest()->isXmlHttpRequest())
65 65
     {
@@ -94,7 +94,12 @@ class SearchController extends Controller
94 94
         $tags_response[] = $tag['name'];
95 95
       }
96 96
       
97
-      $response = new Response(json_encode($tags_response));
97
+      $return_array = array(
98
+        'timestamp' => $timestamp,
99
+        'data'      => $tags_response
100
+      );
101
+      
102
+      $response = new Response(json_encode($return_array));
98 103
       $response->headers->set('Content-Type', 'application/json; charset=utf-8');
99 104
       return $response;
100 105
     }

+ 1 - 1
src/Muzich/CoreBundle/Resources/config/routing.yml View File

@@ -13,7 +13,7 @@ element_add:
13 13
   defaults: { _controller: MuzichCoreBundle:Core:elementAdd, group_slug: null }
14 14
 
15 15
 search_tag:
16
-  pattern: /search/tag/{string_search}
16
+  pattern: /search/tag/{string_search}/{timestamp}
17 17
   defaults: { _controller: MuzichCoreBundle:Search:searchTag, string_search: null }
18 18
   
19 19
 search_tag_ig_by_name:

+ 12 - 3
src/Muzich/CoreBundle/Resources/views/Tag/tagsPrompt.html.twig View File

@@ -31,6 +31,8 @@
31 31
   options.form_name  = "{{ form_name }}";
32 32
   options.tag_init   = taginit;
33 33
   
34
+  ajax_query_timestamp = null;
35
+  
34 36
   $("#tags_prompt_list_{{ form_name }}").tagBox(options);
35 37
   
36 38
   // On détruit la variable taginit
@@ -55,9 +57,16 @@
55 57
         // ici il faut faire une ajax q pour obtenir la liste
56 58
         // ou a est un tableau de strings
57 59
         $('#tag_loader_{{ form_name }}').css('display', 'block');
58
-        $.getJSON('/app_dev.php/fr/search/tag/'+req.term, function(data) {
59
-          responseFn( data );
60
-          $('#tag_loader_{{ form_name }}').css('display', 'none');
60
+        ajax_query_timestamp = new Date().getTime();
61
+        $.getJSON('/app_dev.php/fr/search/tag/'+req.term+'/'+ajax_query_timestamp, function(data) {
62
+          
63
+          // Ce contrôle permet de ne pas continuer si une requete
64
+          // ajax a été faite depuis.
65
+          if (data.timestamp == ajax_query_timestamp)
66
+          {
67
+            responseFn( data.data );
68
+            $('#tag_loader_{{ form_name }}').css('display', 'none');
69
+          }
61 70
         });
62 71
       },
63 72
       minLength: 2,