Add cache_response option to completable form fields

When `cache_response` is set to `false` for completable fields the returned options will not be
cached on the client side. The default is `true`.

Clearing the cache is needed when the completable response depends on a different option of the Agent.

Usage Example:

```
form_configurable :models, roles: :completable, cache_response: false
```

* Disable the completion callback when `select2` is opened programmatically to avoid an infinite loop
* Moved the completion ajax call from `select2-open` to `select2-opening` to clear the cached results before
 `completableDefaultOptions` is called (shows the "Loading ..." message instead of the previous results)
This commit is contained in:
Dominik Sander 2016-09-07 14:53:54 +02:00
parent e114202f5f
commit 3e76fa0e41
3 changed files with 10 additions and 5 deletions

View file

@ -53,10 +53,13 @@ $ ->
updateDropdownData = (form_data, element, data) ->
returnedResults[form_data.attribute] = {text: 'Options', children: data}
$(element).trigger('change')
$("input[role~=completable]").off 'select2-opening', select2OpeningCallback
$(element).select2('open')
$("input[role~=completable]").on 'select2-opening', select2OpeningCallback
$("input[role~=completable]").on 'select2-open', (e) ->
select2OpeningCallback = (e) ->
form_data = getFormData(e.currentTarget)
delete returnedResults[form_data.attribute] if returnedResults[form_data.attribute] && !$(e.currentTarget).data('cacheResponse')
return if returnedResults[form_data.attribute]
$.ajax '/agents/complete',
@ -67,10 +70,12 @@ $ ->
error: (data) ->
updateDropdownData(form_data, e.currentTarget, [{id: undefined, text: 'Error loading data.'}])
$("input[role~=completable]").on 'select2-opening', select2OpeningCallback
$("input[type=radio][role~=form-configurable]").change (e) ->
input = $(e.currentTarget).parents().siblings("input[data-attribute=#{$(e.currentTarget).data('attribute')}]")
if $(e.currentTarget).val() == 'manual'
input.removeClass('hidden')
else
input.val($(e.currentTarget).val())
input.addClass('hidden')
input.addClass('hidden')

View file

@ -32,7 +32,7 @@ module FormConfigurable
options = args.extract_options!.reverse_merge(roles: [], type: :string)
if args.all? { |arg| arg.is_a?(Symbol) }
options.assert_valid_keys([:type, :roles, :values, :ace])
options.assert_valid_keys([:type, :roles, :values, :ace, :cache_response])
end
if options[:type] == :array && (options[:values].blank? || !options[:values].is_a?(Array))

View file

@ -43,7 +43,7 @@ class FormConfigurableAgentPresenter < Decorator
@view.concat(@view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => "form-control #{@agent.send(:boolify, value) != nil ? 'hidden' : ''}"))
end
when :array, :string
@view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => 'form-control')
@view.text_field_tag "agent[options][#{attribute}]", value, html_options.deep_merge(:class => 'form-control', data: {cache_response: data[:cache_response] != false})
end
end
end
end