2016-09-04 7 views
0

古い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>&nbsp;</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を使用してページをリフレッシュすると、その行は削除されたため消えてしまいます。これは正しい動作でなければなりません。

質問:私の解決策は何ですか? 2番目のAJAX呼び出しが行われたときにコントローラかjQueryコードからページを強制的に更新する方法はありますか?

答えて

1

あなたdelete_attrPersonController->deleteAction()に呼び出しを行うURLがある場合は、あなたの問題お使いのブラウザが(リダイレクトされている)Ajaxリクエストを行いますが、返されたデータは、あなたのAJAX呼び出し(success: function (result) {)とページのユーザーに戻ってきているということですcurrenltly見ることは同じです。あなたは何ができるか

は、新しい変数(do_redirect => true)とあなたがあなたのユーザーをリダイレクトするURLを追加し、あなたの成功の関数がそのURLを取得する場合である - それは、リダイレクトを実行する必要があります。

class PersonController extends Zend_Controller_Action { 
    ... 
    // this will delete the person from DB and will make a redirection to indexAction 
    public function deleteAction() 
    { 
     if ($this->getRequest()->isPost()) { 
      ... 
      $person->deletePerson($person_id); 

      return $this->_helper->json(
       ['do_redirect' => TRUE, 'redirect_url' => 'index.php'] 
      ); 
     } 
    } 
} 

そして、あなたのajaxコールでdo_redirectを確認する必要があります:

$.ajax({ 
    url: delete_attr, 
    data: {'id': id}, 
    success: function (result) { 
     if ('do_redirect' in result && 'redirect_url' in result) { 
      window.location.href = result.redirect_url 
     } 
     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', 
       success: function(result) { 
        if ('do_redirect' in result && 'redirect_url' in result) { 
         window.location.href = result.redirect_url 
        } 
       } 
      }); 
     } 
    } 
}); 
関連する問題