Я работаю над некоторыми исправлениями ошибок для старого проекта Zend Framework 1.10, и у меня возникают проблемы с перенаправлением и обновлением страницы.Обновление служебной страницы при перенаправлении
Проблема: сделать звонок AJAX, проверить, назначено ли лицо по страхованию, и если это не позволяет удалить человека до тех пор, пока у него не будет никаких страховок.
Решение:
Контроллер: crud/application/controllers/personController.php
class PersonController extends Zend_Controller_Action
{
// this will fetch all the persons from DB and send to the view
public function indexAction()
{
$persons = new Application_Model_DbTable_Person();
$this->view->persons = $persons->fetchAll();
}
// this will check whether the person has or not insurances
public function hasinsurancesAction()
{
$hasInsurances = new Application_Model_DbTable_Person();
return $this->_helper->json(
['count' => count($hasInsurances->personHasInsurances($this->_getParam('id')))]
);
}
...
// this will delete the person from DB and will make a redirection to indexAction
public function deleteAction()
{
if ($this->getRequest()->isPost()) {
$person_id = (int) $this->getRequest()->getPost('id');
$person = new Application_Model_DbTable_Person();
$person->deletePerson($person_id);
$this->_helper->redirector('index');
}
}
}
Вид: crud/application/views/scripts/company/index.phtml
<table>
<tr>
<th>Title</th>
<th> </th>
</tr>
<?php foreach ($this->persons as $person) : ?>
<tr>
<td><?php echo $this->escape($person->title); ?></td>
<td>
<a href="<?php echo $this->url(
[
'controller' => 'person',
'action' => 'edit',
'id' => $person->id,
]
); ?>">Edit</a>
<a class="delete"
data-id="<?php echo $person->id ?>"
data-href="<?php echo $this->url(
[
'controller' => 'person',
'action' => 'delete',
'id' => $person->id,
]
); ?>"
data-delete-href="<?php echo $this->url(
[
'controller' => 'person',
'action' => 'hasInsurances',
]
); ?>"
href="#">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
JavaScript/JQuery: crud/public/js/delete.js
$(function() {
$('.delete').on('click', function() {
id = $(this).data('id');
href_attr = $(this).data('href');
delete_attr = $(this).data('delete-href');
$.ajax({
url: delete_attr,
data: {'id': id},
success: function (result) {
if (result.count > 0) {
alert('You can not delete this person. Try deleting associated insurances first.')
} else {
$.ajax({
url: href_attr,
data: {'id': id},
type: 'POST'
});
}
}
});
})
});
Вопрос: приведенный выше код работает отлично, но имеет разрыв, когда deleteAction()
называется и человек будет удален он пробует перенаправление indexAction()
там я до сих пор видим уничтоженный человека, и я не должен. Как только я обновляю страницу с помощью F5
или CTRL+R
, строка исчезает, потому что она была удалена, и это должно быть правильное поведение.
Вопрос: Что не так в моем решении? есть ли способ принудительного обновления страницы либо с контроллера, либо с помощью кода jQuery при выполнении второго вызова AJAX?