Jelajahi Sumber

[autocomplete] Allow to configure if the browser should cache the
requested pages

Bruno Galeotti 9 tahun lalu
induk
melakukan
3b9d011380

+ 2 - 0
Form/Type/ModelAutocompleteType.php

@@ -74,6 +74,7 @@ class ModelAutocompleteType extends AbstractType
         $view->vars['req_param_name_page_number'] = $options['req_param_name_page_number'];
         $view->vars['req_param_name_page_number'] = $options['req_param_name_page_number'];
         $view->vars['req_param_name_items_per_page'] = $options['req_param_name_items_per_page'];
         $view->vars['req_param_name_items_per_page'] = $options['req_param_name_items_per_page'];
         $view->vars['quiet_millis'] = $options['quiet_millis'];
         $view->vars['quiet_millis'] = $options['quiet_millis'];
+        $view->vars['cache'] = $options['cache'];
 
 
         // CSS classes
         // CSS classes
         $view->vars['container_css_class'] = $options['container_css_class'];
         $view->vars['container_css_class'] = $options['container_css_class'];
@@ -122,6 +123,7 @@ class ModelAutocompleteType extends AbstractType
             'minimum_input_length'          => 3, //minimum 3 chars should be typed to load ajax data
             'minimum_input_length'          => 3, //minimum 3 chars should be typed to load ajax data
             'items_per_page'                => 10, //number of items per page
             'items_per_page'                => 10, //number of items per page
             'quiet_millis'                  => 100,
             'quiet_millis'                  => 100,
+            'cache'                         => false,
 
 
             'to_string_callback'            => null,
             'to_string_callback'            => null,
 
 

+ 3 - 0
Resources/doc/reference/form_types.rst

@@ -255,6 +255,9 @@ items_per_page
 quiet_millis
 quiet_millis
   defaults to 100. Number of milliseconds to wait for the user to stop typing before issuing the ajax request.
   defaults to 100. Number of milliseconds to wait for the user to stop typing before issuing the ajax request.
 
 
+cache
+  defaults to false. Set to true, if the requested pages should be cached by the browser.
+
 url
 url
   defaults to "". Target external remote URL for ajax requests.
   defaults to "". Target external remote URL for ajax requests.
   You usually should not need to set this manually.
   You usually should not need to set this manually.

+ 1 - 0
Resources/views/Form/Type/sonata_type_model_autocomplete.html.twig

@@ -45,6 +45,7 @@ file that was distributed with this source code.
                     url:  '{{ url ?: path(route.name, route.parameters|default([]))|e('js') }}',
                     url:  '{{ url ?: path(route.name, route.parameters|default([]))|e('js') }}',
                     dataType: 'json',
                     dataType: 'json',
                     quietMillis: {{ quiet_millis }},
                     quietMillis: {{ quiet_millis }},
+                    cache: {{ cache ? 'true' : 'false' }},
                     data: function (term, page) { // page is the one-based page number tracked by Select2
                     data: function (term, page) { // page is the one-based page number tracked by Select2
                         {% block sonata_type_model_autocomplete_ajax_request_parameters %}
                         {% block sonata_type_model_autocomplete_ajax_request_parameters %}
                         return {
                         return {

+ 1 - 0
Tests/Form/Type/ModelAutocompleteTypeTest.php

@@ -39,6 +39,7 @@ class ModelAutocompleteTypeTest extends TypeTestCase
         $this->assertSame(3, $options['minimum_input_length']);
         $this->assertSame(3, $options['minimum_input_length']);
         $this->assertSame(10, $options['items_per_page']);
         $this->assertSame(10, $options['items_per_page']);
         $this->assertSame(100, $options['quiet_millis']);
         $this->assertSame(100, $options['quiet_millis']);
+        $this->assertSame(false, $options['cache']);
         $this->assertSame('', $options['width']);
         $this->assertSame('', $options['width']);
         $this->assertFalse($options['dropdown_auto_width']);
         $this->assertFalse($options['dropdown_auto_width']);