Skip to content

Commit

Permalink
Added callback after ajax request
Browse files Browse the repository at this point in the history
  • Loading branch information
damianociarla committed Apr 17, 2014
1 parent 93fdbc8 commit f3de054
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ vendor/*
web/uploads/*
web/bundles/*

.idea

# Configuration files
app/config/parameters.ini
app/config/parameters.yml
Expand Down
13 changes: 13 additions & 0 deletions Form/Type/SelectCityFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormView;
use DCS\Form\SelectCityFormFieldBundle\Model;

class SelectCityFormType extends AbstractType
Expand Down Expand Up @@ -37,6 +38,14 @@ public function __construct(
$this->cityManager = $cityManager;
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars = array_merge($view->vars, array(
'callback_country' => $options['callback_country'],
'callback_region' => $options['callback_region'],
));
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
Expand Down Expand Up @@ -135,12 +144,16 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'country_required' => true,
'region_required' => true,
'city_required' => true,
'callback_country' => 'callbackCountry',
'callback_region' => 'callbackRegion',
));

$resolver->setAllowedTypes(array(
'country_required' => 'bool',
'region_required' => 'bool',
'city_required' => 'bool',
'callback_country' => 'string',
'callback_region' => 'string',
));
}

Expand Down
20 changes: 16 additions & 4 deletions Resources/public/js/select_city.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
window.select_region_base_url = null;
window.select_city_base_url = null;
window.select_city_instance = [];
window.select_city = function (countryId, regionId, cityId) {
window.select_city = function (countryId, regionId, cityId, callbackCountry, callbackRegion) {
var countryElement = document.getElementById(countryId);
var regionElement = document.getElementById(regionId);
var cityElement = document.getElementById(cityId);

var loadJsonData = function (url, element, param) {
var loadJsonData = function (url, element, param, type) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
Expand All @@ -24,6 +24,18 @@ window.select_city = function (countryId, regionId, cityId) {
option.value = info.id;
element.appendChild(option);
}
switch (type) {
case 'country':
if (callbackCountry !== null) {
callbackCountry(data, element);
}
break;
case 'region':
if (callbackRegion !== null) {
callbackRegion(data, element);
}
break;
}
}
};

Expand All @@ -39,12 +51,12 @@ window.select_city = function (countryId, regionId, cityId) {
var url = window.select_region_base_url.replace("countryId", countryElement.options[countryElement.selectedIndex].value);
emptyOptionFromSelect(regionElement);
emptyOptionFromSelect(cityElement);
loadJsonData(url, regionElement, 'regionName');
loadJsonData(url, regionElement, 'regionName', 'country');
};

regionElement.onchange = function () {
var url = window.select_city_base_url.replace("regionId", regionElement.options[regionElement.selectedIndex].value);
emptyOptionFromSelect(cityElement);
loadJsonData(url, cityElement, 'cityName');
loadJsonData(url, cityElement, 'cityName', 'region');
};
};
18 changes: 10 additions & 8 deletions Resources/views/Form/select_city.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
{% spaceless %}
{{ form_widget(form) }}
<script type="text/javascript">
window.onload = function () {
if (window.select_region_base_url === null) {
window.select_region_base_url = '{{ path("dcs_select_city_form_field_api_regions", {'countryId': 'countryId'}) }}';
window.onload = function () {
if (window.select_region_base_url === null) {
window.select_region_base_url = '{{ path("dcs_select_city_form_field_api_regions", {'countryId': 'countryId'}) }}';
}
if (window.select_city_base_url === null) {
window.select_city_base_url = '{{ path("dcs_select_city_form_field_api_cities", {'regionId': 'regionId'}) }}';
}
var callbackCountry = typeof {{ form.vars.id ~ '_' ~ callback_country }} != 'undefined' ? {{ form.vars.id ~ '_' ~ callback_country }} : null;
var callbackRegion = typeof {{ form.vars.id ~ '_' ~ callback_region }} != 'undefined' ? {{ form.vars.id ~ '_' ~ callback_region }} : null;
window.select_city_instance.push(new window.select_city('{{ form.country.vars.id }}', '{{ form.region.vars.id }}', '{{ form.city.vars.id }}', callbackCountry, callbackRegion));
}
if (window.select_city_base_url === null) {
window.select_city_base_url = '{{ path("dcs_select_city_form_field_api_cities", {'regionId': 'regionId'}) }}';
}
window.select_city_instance.push(new window.select_city('{{ form.country.vars.id }}', '{{ form.region.vars.id }}', '{{ form.city.vars.id }}'));
}
</script>
{% endspaceless %}
{% endblock %}

0 comments on commit f3de054

Please sign in to comment.